版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/leekerian/article/details/81571891
题意:有n个国家,输入国家的坐标让你构建国家的凸包,然后输入炮弹,当炮弹在这个国家里的话国家就被炸毁了求最后被炸毁的总面积。
wa的点!!!!有可能反复炸一个国家不能重复计算面积
#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
#include <cstdio>
using namespace std;
const int maxn=200;
const double eps=1e-8;
int sgn(double x)
{
if(fabs(x)<eps)
return 0;
if(x<0)
return -1;
else
return 1;
}
struct Point
{
double x,y;
Point(){}
Point(double _x,double _y)
{
x=_x;
y=_y;
}
Point operator -(const Point &b)const
{
return Point(x-b.x,y-b.y);
}
double operator ^(const Point &b)const
{
return x*b.y-y*b.x;
}
double operator *(const Point &b)const
{
return x*b.x+y*b.y;
}
};
struct Line
{
Point s,e;
Line(){}
Line(Point _s,Point _e)
{
s=_s;
e=_e;
}
};
int n;
Point p[maxn];
int Stack[maxn];
int top=0;
double dist(Point a,Point b)
{
return sqrt((a-b)*(a-b));
}
bool cmp(Point a,Point b)
{
int ans=sgn((a-p[0])^(b-p[0]));
if(ans==1)
return true;
else if(ans==0)
return dist(a,p[0])<dist(b,p[0]);
else
return false;
}
void graham()
{
for(int i=0;i<n;i++)
{
if(p[i].y<p[0].y||(p[i].y==p[0].y&&p[i].x<p[0].x))
swap(p[i],p[0]);
}
sort(p+1,p+n,cmp);
if(n==1)
{
Stack[0]=0;
top=1;
}
else if(n==2)
{
Stack[0]=0;
Stack[1]=1;
top=2;
}
else
{
Stack[0]=0;
Stack[1]=1;
top=2;
for(int i=2;i<n;i++)
{
while(top>1&&((p[Stack[top-1]]-p[Stack[top-2]])^(p[i]-p[Stack[top-2]]))<=0)
top--;
Stack[top++]=i;
}
}
}
vector <Point> ve[maxn];
double CalcArea(vector <Point> &ve)
{
double res=0;
for(int i=0;i<ve.size();i++)
{
res+=(ve[i]^ve[(i+1)%ve.size()])/2;
}
return fabs(res);
}
bool OnSeg(Point p,Line l)
{
return sgn((l.s-p)^(l.e-p))==0&&
sgn((p.x-l.s.x)*(p.x-l.e.x))<=0&&
sgn((p.y-l.s.y)*(p.y-l.e.y))<=0;
}
int inConvexPoly(Point p1,vector <Point> &ve)
{
for(int i=0;i<ve.size();i++)
{
if(sgn((ve[i]-p1)^(ve[(i+1)%(ve.size())]-p1))<0)
return -1;
else if(OnSeg(p1,Line(ve[i],ve[(i+1)%(ve.size())])))
return 0;
}
return 1;
}
int main()
{
int cont=0;
double ans=0;
while(1)
{
scanf("%d",&n);
if(n==-1)
break;
for(int i=0;i<n;i++)
scanf("%lf %lf",&p[i].x,&p[i].y);
top=0;
graham();
for(int i=0;i<top;i++)
{
ve[cont].push_back(p[Stack[i]]);
}
//ans+=CalcArea(ve[cont]);
cont++;
}
//printf("%.2lf\n",ans);
double x,y;
int check[maxn]={0};
while(scanf("%lf %lf",&x,&y)!=EOF)
{
Point p1=Point(x,y);
for(int i=0;i<cont;i++)
{
if(inConvexPoly(p1,ve[i])>=0&&check[i]==0)
{
check[i]=1;
ans+=CalcArea(ve[i]);
continue;
}
}
}
printf("%.2lf\n",ans);
return 0;
}