uva 109 SCUD Busters-AC-Upgraded version
来源:互联网 发布:淘宝手机端网页版 编辑:程序博客网 时间:2024/05/16 13:02
//uva 109 SCUD Busters-AC-Upgraded version //AC By Warteac//Runtime:0.009s//2013-5-16/*输入: 第一行的整数,表示输入点的个数 的二行开始,每两个整数表示一个点的坐标 最后一行可以是第一个点(封闭型),也可以是最后一个点(不封闭),但都表示一个封闭的多边形 可以多点共线结果: 凸包的结果存放于grham_scan()的第二个参数 凸包的顺序是逆时针 构造成功返回true*/ #include<iostream>#include<cstdio>#include<vector>#include<stack>#include <iomanip>#include<algorithm>#include<cmath>using namespace std;typedef int PType;//定义点的类型///////////////////////////////////////struct Point{ PType x,y; Point(PType a = 0, PType b = 0){x = a; y = b;} void print(){cout << "x = " << x << "y = " << y << endl;} Point operator -(Point p){return Point(x - p.x, y - p.y);} }pBase;//最左下的点typedef Point Vector;//向量int direction(Vector v1,Vector v2){//求两个向量的叉积 return (v1.x*v2.y-v1.y*v2.x); }bool cmp(Point p1, Point p2){//比较函数 int d = direction(p1 - pBase, p2 - pBase); if(d == 0){//极角相同 return abs(p1.x) < abs(p2.x);//距离从小到大排 }else { return d < 0;//按逆时针排序 }}bool grham_scan(vector <Point> p , vector <Point> &parray){//求凸包的算法//find first point p0 with minimum y-coordinate or minimun x-coordinate if y are the same int index = 0; for(int i = 1; i < p.size(); i++){ if(p[i].y < p[index].y ||(p[i].y == p[index].y && p[i].x < p[index].x)) index = i; } swap(p[0],p[index]); pBase = p[0]; sort(p.begin()+1,p.end(),cmp);//get the convex hull from p parray.clear(); parray.push_back(p[0]); parray.push_back(p[1]); p.push_back(p[0]); for(int i = 2; i < p.size(); i++){ Vector v1 (p[i].x - parray.back().x, p[i].y - parray.back().y); Vector v2 (parray.back().x - (parray.end()-2)->x, parray.back().y - (parray.end()-2)->y); int d = direction(v2,v1); if(d <= 0) {parray.push_back(p[i]);} else if(d > 0){ parray.pop_back();i--; } } return true;}int areaOfPolygon(vector <Point> &vecCH) {//求凸多边形的面积 int nArea = 0; for (vector <Point>::iterator i = vecCH.begin(); i != vecCH.end() - 1; ++i) { nArea += (i + 1)->x * i->y - i->x * (i + 1)->y; } return nArea;}bool inPolygon(Point pt, vector <Point> &vecCH) {//判断点是否在多边形内部 for (vector <Point>::iterator i = vecCH.begin(); i != vecCH.end() - 1; ++i) { int nX1 = pt.x - i->x, nY1 = pt.y - i->y; int nX2 = (i + 1)->x - i->x, nY2 = (i + 1)->y - i->y; if (nX1 * nY2 - nY1 * nX2 < 0) { return false; } } return true;}//////////////////////////////////////////////////////int main(){ int sites; PType x,y; vector <Point> v; Point t,mi; vector < vector <Point> > p;//convex hull int aflag[105] = {0}; int area = 0; //input kingdom while(cin >> sites && sites != -1){ v.clear(); vector <Point> temp;//one convex hull while(sites--){ cin >> t.x >> t.y; v.push_back(t); } //computing convex hull if(grham_scan(v,temp) == true){ p.push_back(temp);//all convex hull } } //input missile landings while(cin >> mi.x >> mi.y){ for(int j = 0; j < p.size(); j++){ if((inPolygon(mi,p[j]) == true) && aflag[j] != 1){ aflag[j] = 1; area += areaOfPolygon(p[j]); break; } } } cout << setiosflags(ios::fixed) << setprecision(2); cout << (double)area/2.0 << endl; return 0;}