POJ 1264 判断点是否在凸多边形内

来源:互联网 发布:手机更换ip地址软件 编辑:程序博客网 时间:2024/05/18 01:03

SCUD Busters
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 1154 Accepted: 460

Description

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 x 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 (anitary leansing niversal estroyer) 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. 

Input

The input is a sequence of kingdom specifications followed by a sequence of missile landing locations. 
A kingdom is specified by a number N (3 <= N <= 100) 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. 

Output

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.

Sample Input

123 34 64 114 810 65 76 66 37 910 410 91 7520 2020 4040 2040 4030 30310 1021 1021 13-15 520 12

Sample Output

70.50


#include<iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<cmath>#include<algorithm>#define inf 0x7fffffff#define exp 1e-10#define PI 3.141592654using namespace std;const int MAXN=111;struct Point{    double x,y;    Point (double x=0,double y=0):x(x),y(y){}};typedef Point Vector;struct Line{    Point p;    Vector v;    double ang;    Line (){}    Line (Point p,Vector v):p(p),v(v){ang=atan2(v.y,v.x); }    //Line (Point p,Vector v):p(p),v(v){ang=atan2(v.y,v.x); }    friend bool operator < (Line a,Line b)    {        return a.ang<b.ang;    }};Vector operator + (Vector A,Vector B) {return Vector(A.x+B.x , A.y+B.y); }Vector operator - (Vector A,Vector B) {return Vector(A.x-B.x , A.y-B.y); }Vector operator * (Vector A,double p) {return Vector(A.x*p , A.y*p); }Vector operator / (Vector A,double p) {return Vector(A.x/p , A.y/p); }int dcmp(double x) {if (fabs(x)<exp) return 0;return x>0 ? 1 : -1; }double cross(Vector A,Vector B){    return A.x*B.y-B.x*A.y;}bool OnLeft(Line L,Point p){    return cross(L.v,p-L.p)>=0;///点P在有向直线L的左边(>=0说明在线上也算)}Point GetIntersection(Line a,Line b){    Vector u=a.p-b.p;    double t=cross(b.v,u)/cross(a.v,b.v);    return a.p+a.v*t;}double dist(Point A,Point B){return sqrt(  (double) ( 1.0*(A.x-B.x)*(A.x-B.x)+1.0*(A.y-B.y)*(A.y-B.y) ) );}bool cmp(Point A,Point B){long long tmp=cross( A , B );if (tmp>0)    return 1;if (tmp<0)    return 0;if (tmp==0)    return (A.x*A.x)+(A.y*A.y)<(B.x*B.x)+(B.y*B.y);}int ConvexHull(Point *a,Point *poly,int n,Point &Base){///a 为传入的点集,poly为求出的凸包上的点集Point S;S.x=1000000;///x坐标的最大值for (int i=0;i<n;i++)    {        if ( (a[i].x<S.x)|| ( (a[i].x==S.x)&& (a[i].y<S.y) ) )            S=a[i];    }for (int i=0;i<n;i++)    a[i]=a[i]-S;///把最左的点中最下的点作为原点求出其他点的相对位置Base=S;sort(a,a+n,cmp);///顺时针排序int *stack=new int[n];int top=0;stack[0]=0;for (int i=1;i<n;i++)    {        while ( ( top>=2) && ( cross( a[i]-a[ stack[ top ] ],a[ stack[ top-1 ] ]-a[ stack[ top ] ] )<=0ll ) )            top--;        stack[++top]=i;    }for (int i=0;i<=top;i++)    poly[i]=a[ stack [ i ] ];return top+1;}///凸包模板double ans;int cnt,n;Point S;Point na[MAXN];struct convex{Point p[MAXN];Point Base;int tot;double s;}a[MAXN];void init(){for (int i=0;i<n;i++)    {        scanf("%lf%lf",&na[i].x,&na[i].y);        //printf("%lf %lf\n",na[i].x,na[i].y);    }a[cnt].tot=ConvexHull( na , a[cnt].p , n , a[cnt].Base);a[cnt].s=0;for (int i=1;i<a[cnt].tot-1;i++)    a[cnt].s+=cross( a[cnt].p[i] , a[cnt].p[i+1] );a[cnt].s/=2.0;}bool vis[MAXN];int get(Point *a,int n,Point x){int l=0,r=n-1;int tmp=0;while (r>=l){int mid=(l+r)/2;if (  dcmp( cross( x ,a[mid] ) ) <= 0){l=mid+1;tmp=mid;}elser=mid-1;}return tmp;}int main(){    while (scanf("%d",&n)!=EOF)        {            if (n==-1)                break;            init();            ++cnt;        }    Point tmp;    while ( scanf("%lf%lf",&tmp.x,&tmp.y)!=EOF )        {            bool flag=0;            for (int i=0;( i<cnt ) && (!flag) ;i++)                if (!vis[i])                {                int j=get( a[i].p, a[i].tot ,  tmp - a[i].Base );                        if ( ( dcmp( cross( a[i].p[j] ,tmp-a[i].Base ) ) >=0 ) && (  dcmp( cross( a[i].p[j+1] , tmp-a[i].Base ) ) <=0) )                            if ( ( dcmp( cross ( tmp-a[i].Base-a[i].p[j] ,a[i].p[j+1]-a[i].p[j]) )<=0) )                            {                                vis[i]=1;                                flag=1;                                ans+=a[i].s;                            }                }        }    printf("%.2f\n",ans);    return 0;}


 
阅读全文
0 0
原创粉丝点击