uva 109 SCUD Busters-AC-Upgraded version

//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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值