題目來源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&page=show_problem&problem=45
Some problems are difficult to solve but have a simplification that is easy to solve. Rather than deal with the difficulties of constructing a model of the Earth (a somewhat oblate spheroid), consider a pre-Columbian flat world that is a 500 kilometer 500 kilometer square.
In the model used in this problem, the flat world consists of several warring kingdoms. Though warlike, the people of the world are strict isolationists; each kingdom is surrounded by a high (but thin) wall designed to both protect the kingdom and to isolate it. To avoid fights for power, each kingdom has its own electric power plant.
When the urge to fight becomes too great, the people of a kingdom often launch missiles at other kingdoms. Each SCUD missile (Sanitary Cleansing Universal Destroyer) that lands within the walls of a kingdom destroys that kingdom's power plant (without loss of life).
Given coordinate locations of several kingdoms (by specifying the locations of houses and the location of the power plant in a kingdom) and missile landings you are to write a program that determines the total area of all kingdoms that are without power after an exchange of missile fire.
In the simple world of this problem kingdoms do not overlap. Furthermore, the walls surrounding each kingdom are considered to be of zero thickness. The wall surrounding a kingdom is the minimal-perimeter wall that completely surrounds all the houses and the power station that comprise a kingdom; the area of a kingdom is the area enclosed by the minimal-perimeter thin wall.
There is exactly one power station per kingdom.
There may be empty space between kingdoms.
The input is a sequence of kingdom specifications followed by a sequence of missile landing locations.
A kingdom is specified by a number N ( ) on a single line which indicates the number of sites in this kingdom. The next line contains the x and y coordinates of the power station, followed by N-1 lines of x, y pairs indicating the locations of homes served by this power station. A value of -1 for N indicates that there are no more kingdoms. There will be at least one kingdom in the data set.
Following the last kingdom specification will be the coordinates of one or more missile attacks, indicating the location of a missile landing. Each missile location is on a line by itself. You are to process missile attacks until you reach the end of the file.
Locations are specified in kilometers using coordinates on a 500 km by 500 km grid. All coordinates will be integers between 0 and 500 inclusive. Coordinates are specified as a pair of integers separated by white-space on a single line. The input file will consist of up to 20 kingdoms, followed by any number of missile attacks.
The output consists of a single number representing the total area of all kingdoms without electricity after all missile attacks have been processed. The number should be printed with (and correct to) two decimal places.
12 3 3 4 6 4 11 4 8 10 6 5 7 6 6 6 3 7 9 10 4 10 9 1 7 5 20 20 20 40 40 20 40 40 30 30 3 10 10 21 10 21 13 -1 5 5 20 12
70.50
You may or may not find the following formula useful.
Given a polygon described by the vertices such that , the signed area of the polygon is given by
where the x, y coordinates of ; the edges of the polygon are from to for .
If the points describing the polygon are given in a counterclockwise direction, the value of a will be positive, and if the points of the polygon are listed in a clockwise direction, the value of a will be negative.
推薦博客:http://www.cnblogs.com/devymex/archive/2010/08/09/1795391.html
解題思路:
計算幾何類型的題目。需要用到三個基本算法,一是求凸包,二是判斷點在多邊形內,三是求多邊形面積(題目中已給出)。關於凸包算法請詳見Graham's Scan法。判斷點在多邊形內的算法有很多種,這裡用到的是外積法:設待判斷的點為p,逆時針或順時針遍例多邊形的每個點vn,將兩個向量<p, vn>和<vn, vn + 1>做外積。如果對於多邊形上所有的點,外積的符號都相同(順時針為負,逆時針為正),則可斷定p在多邊形內。外積出現0,則表示p在邊上,否則在多邊形外。
算法的思路很直接,實現也很簡單,關鍵是這道題的測試數據太扯蛋了,讓我郁悶了很久。題目中並未說明導彈打在牆上怎麼辦,只是說“... whithin the wall ...”。根據測試結果來看,打在牆上和打在據點上都要算打中。題目中還提到國家不會互相重疊“... kingdoms do not overlap.”,但測試表明數據裡確有重疊的情況,因此在導彈擊中後一定要跳出循環,否則會出現一彈多擊的情況。
給你N個王國,求下凸包,再求面積。給你一些炮彈,問炮彈炸掉的面積。(一個炮彈炸的話,整個王國都被炸了)。
直接求凸包後,求出各個王國的面積,然後判斷炮彈在哪個王國裡,這個直接用判斷點是否在多邊形內。
參考代碼:
1 #include<bits/stdc++.h> 2 3 using namespace std; 4 5 const int MAX = 110; 6 struct point{ double x,y;}; //點 7 struct polygon{ point c[MAX],a; double area; int n;}; 8 struct segment{ point a,b;}; // 線段 9 const double eps = 1e-6; 10 bool dy(double x,double y) { return x > y + eps;} // x > y 11 bool xy(double x,double y) { return x < y - eps;} // x < y 12 bool dyd(double x,double y) { return x > y - eps;} // x >= y 13 bool xyd(double x,double y) { return x < y + eps;} // x <= y 14 bool dd(double x,double y) { return fabs( x - y ) < eps;} // x == y 15 polygon king[MAX]; 16 point c[MAX]; 17 double crossProduct(point a,point b,point c)//向量 ac 在 ab 的方向 18 { 19 return (c.x - a.x)*(b.y - a.y) - (b.x - a.x)*(c.y - a.y); 20 } 21 double disp2p(point a,point b) 22 { 23 return sqrt( ( a.x - b.x ) * ( a.x - b.x ) + ( a.y - b.y ) * ( a.y - b.y ) ); 24 } 25 double area_polygon(point p[],int n) 26 { 27 double s = 0.0; 28 for(int i=0; i<n; i++) 29 s += p[(i+1)%n].y * p[i].x - p[(i+1)%n].x * p[i].y; 30 return fabs(s)/2.0; 31 } 32 bool cmp(point a,point b) // 排序 33 { 34 double len = crossProduct(c[0],a,b); 35 if( dd(len,0.0) ) 36 return xy(disp2p(c[0],a),disp2p(c[0],b)); 37 return xy(len,0.0); 38 } 39 bool onSegment(point a, point b, point c) 40 { 41 double maxx = max(a.x,b.x); 42 double maxy = max(a.y,b.y); 43 double minx = min(a.x,b.x); 44 double miny = min(a.y,b.y); 45 if( dd(crossProduct(a,b,c),0.0) && dyd(c.x,minx) && xyd(c.x,maxx) && dyd(c.y,miny) && xyd(c.y,maxy) ) 46 return true; 47 return false; 48 } 49 bool segIntersect(point p1,point p2, point p3, point p4) 50 { 51 double d1 = crossProduct(p3,p4,p1); 52 double d2 = crossProduct(p3,p4,p2); 53 double d3 = crossProduct(p1,p2,p3); 54 double d4 = crossProduct(p1,p2,p4); 55 if( xy(d1 * d2,0.0) && xy(d3 * d4,0.0) ) return true; 56 if( dd(d1,0.0) && onSegment(p3,p4,p1) ) return true;//如果不判端點相交,則下面這四句話不需要 57 if( dd(d2,0.0) && onSegment(p3,p4,p2) ) return true; 58 if( dd(d3,0.0) && onSegment(p1,p2,p3) ) return true; 59 if( dd(d4,0.0) && onSegment(p1,p2,p4) ) return true; 60 return false; 61 } 62 bool point_inPolygon(point pot,point p[],int n) 63 { 64 int count = 0; 65 segment l; 66 l.a = pot; 67 l.b.x = 1e10*1.0; 68 l.b.y = pot.y; 69 p[n] = p[0]; 70 for(int i=0; i<n; i++) 71 { 72 if( onSegment(p[i],p[i+1],pot) ) 73 return true; 74 if( !dd(p[i].y,p[i+1].y) ) 75 { 76 int tmp = -1; 77 if( onSegment(l.a,l.b,p[i]) ) 78 tmp = i; 79 else 80 if( onSegment(l.a,l.b,p[i+1]) ) 81 tmp = i+1; 82 if( tmp != -1 && dd(p[tmp].y,max(p[i].y,p[i+1].y)) ) 83 count++; 84 else 85 if( tmp == -1 && segIntersect(p[i],p[i+1],l.a,l.b) ) 86 count++; 87 } 88 } 89 if( count % 2 == 1 ) 90 return true; 91 return false; 92 } 93 point stk[MAX]; 94 int top; 95 double Graham(int n) 96 { 97 int tmp = 0; 98 for(int i=1; i<n; i++) 99 if( xy(c[i].x,c[tmp].x) || dd(c[i].x,c[tmp].x) && xy(c[i].y,c[tmp].y) ) 100 tmp = i; 101 swap(c[0],c[tmp]); 102 sort(c+1,c+n,cmp); 103 stk[0] = c[0]; stk[1] = c[1]; 104 top = 1; 105 for(int i=2; i<n; i++) 106 { 107 while( xyd( crossProduct(stk[top],stk[top-1],c[i]), 0.0 ) && top >= 1 ) 108 top--; 109 stk[++top] = c[i]; 110 } 111 return area_polygon(stk,top+1); 112 } 113 int main() 114 { 115 int n; 116 int i = 0; 117 double x,y; 118 while( ~scanf("%d",&n) && n != -1 ) 119 { 120 king[i].n = n; 121 for(int k=0; k<n; k++) 122 scanf("%lf %lf",&king[i].c[k].x,&king[i].c[k].y); 123 king[i].a = king[i].c[0]; 124 i++; 125 } 126 127 double sum = 0.0; 128 for(int k=0; k<i; k++) 129 { 130 memcpy(c,king[k].c,sizeof(king[k].c)); 131 king[k].area = Graham(king[k].n); 132 memcpy(king[k].c,stk,sizeof(stk)); 133 king[k].n = top+1; 134 } 135 point pot; 136 bool die[MAX]; 137 memset(die,false,sizeof(die)); 138 while( ~scanf("%lf %lf",&pot.x,&pot.y) ) 139 { 140 for(int k=0; k<i; k++) 141 if( point_inPolygon(pot,king[k].c,king[k].n) && !die[k] ) 142 { 143 die[k] = true; 144 sum += king[k].area; 145 } 146 } 147 148 printf("%.2lf\n",sum); 149 return 0; 150 }
參考代碼2:
1 #include <algorithm> 2 #include <functional> 3 #include <iomanip> 4 #include <iostream> 5 #include <vector> 6 #include <math.h> 7 8 using namespace std; 9 10 struct POINT { 11 int x; int y; 12 bool operator==(const POINT &other) { 13 return (x == other.x && y == other.y); 14 } 15 } ptBase; 16 17 typedef vector<POINT> PTARRAY; 18 19 bool CompareAngle(POINT pt1, POINT pt2) { 20 pt1.x -= ptBase.x, pt1.y -= ptBase.y; 21 pt2.x -= ptBase.x, pt2.y -= ptBase.y; 22 return (pt1.x / sqrt((float)(pt1.x * pt1.x + pt1.y * pt1.y)) < 23 pt2.x / sqrt((float)(pt2.x * pt2.x + pt2.y * pt2.y))); 24 } 25 26 void CalcConvexHull(PTARRAY &vecSrc, PTARRAY &vecCH) { 27 ptBase = vecSrc.back(); 28 sort(vecSrc.begin(), vecSrc.end() - 1, &CompareAngle); 29 vecCH.push_back(ptBase); 30 vecCH.push_back(vecSrc.front()); 31 POINT ptLastVec = { vecCH.back().x - ptBase.x, 32 vecCH.back().y - ptBase.y }; 33 PTARRAY::iterator i = vecSrc.begin(); 34 for (++i; i != vecSrc.end() - 1; ++i) { 35 POINT ptCurVec = { i->x - vecCH.back().x, i->y - vecCH.back().y }; 36 while (ptCurVec.x * ptLastVec.y - ptCurVec.y * ptLastVec.x < 0) { 37 vecCH.pop_back(); 38 ptCurVec.x = i->x - vecCH.back().x; 39 ptCurVec.y = i->y - vecCH.back().y; 40 ptLastVec.x = vecCH.back().x - (vecCH.end() - 2)->x; 41 ptLastVec.y = vecCH.back().y - (vecCH.end() - 2)->y; 42 } 43 vecCH.push_back(*i); 44 ptLastVec = ptCurVec; 45 } 46 vecCH.push_back(vecCH.front()); 47 } 48 49 int CalcArea(PTARRAY &vecCH) { 50 int nArea = 0; 51 for (PTARRAY::iterator i = vecCH.begin(); i != vecCH.end() - 1; ++i) { 52 nArea += (i + 1)->x * i->y - i->x * (i + 1)->y; 53 } 54 return nArea; 55 } 56 57 bool PointInConvexHull(POINT pt, PTARRAY &vecCH) { 58 for (PTARRAY::iterator i = vecCH.begin(); i != vecCH.end() - 1; ++i) { 59 int nX1 = pt.x - i->x, nY1 = pt.y - i->y; 60 int nX2 = (i + 1)->x - i->x, nY2 = (i + 1)->y - i->y; 61 if (nX1 * nY2 - nY1 * nX2 < 0) { 62 return false; 63 } 64 } 65 return true; 66 } 67 68 int main(void) { 69 vector<PTARRAY> vecKingdom; 70 POINT ptIn; 71 int aFlag[100] = {0}, nAreaSum = 0; 72 for (int nPtCnt; cin >> nPtCnt && nPtCnt >= 1;) { 73 PTARRAY vecSrc, vecCH; 74 cin >> ptIn.x >> ptIn.y; 75 vecSrc.push_back(ptIn); 76 for (; --nPtCnt != 0;) { 77 cin >> ptIn.x >> ptIn.y; 78 POINT &ptMin = vecSrc.back(); 79 vecSrc.insert(vecSrc.end() - (ptIn.y > ptMin.y || 80 (ptIn.y == ptMin.y && ptIn.x > ptMin.x)), ptIn); 81 } 82 CalcConvexHull(vecSrc, vecCH); 83 vecKingdom.push_back(vecCH); 84 } 85 while (cin >> ptIn.x >> ptIn.y) { 86 vector<PTARRAY>::iterator i = vecKingdom.begin(); 87 for (int k = 0; i != vecKingdom.end(); ++i, ++k) { 88 if (PointInConvexHull(ptIn, *i) && aFlag[k] != 1) { 89 nAreaSum += CalcArea(*i); 90 aFlag[k] = 1; 91 break; 92 } 93 } 94 } 95 cout << setiosflags(ios::fixed) << setprecision(2); 96 cout << (float)nAreaSum / 2.0f << endl; 97 return 0; 98 }