poj-1474 Video Surveillance

来源:互联网 发布:linux c语言utf8转gbk 编辑:程序博客网 时间:2024/06/05 23:40

题意:

判断多边形是否存在核;

点集顺时针或逆时针给出,n<=100;


题解:

半平面交模板题;

多边形的核就在组成多边形的半平面的交上;

也可以顺便说明多边形的核若存在则一定是凸的;

原因似乎画画图是比较显然的;

一个地方被挡住一定是因为那被另一条边挡住了嘛;


注意半平面交的判断点与直线位置关系要用>=号;

此题买一送二,我大胆地在提交框里改输出然后光荣的WA了= =

poj-1474 poj-3130 poj-3335


代码:


#include<math.h>#include<stdio.h>#include<string.h>#include<algorithm>#define N 110using namespace std;const double EPS=1e-10;const double pi=acos(-1.0);struct Point{double x,y;void read(){scanf("%lf%lf",&x,&y);}Point operator +(Point a){a.x=x+a.x,a.y=y+a.y;return a;}Point operator -(Point a){a.x=x-a.x,a.y=y-a.y;return a;}double operator ^(Point a){return x*a.y-y*a.x;}friend Point operator *(double a,Point b){b.x=a*b.x,b.y=a*b.y;return b;}}a[N],p[N];struct Line{Point p,v;double alpha;void build(Point a,Point b){p=a,v=b-a;alpha=atan2(v.y,v.x);}friend bool operator <(Line a,Line b){return a.alpha<b.alpha;}friend Point Cross(Line a,Line b){Point u=a.p-b.p;double temp=(b.v^u)/(a.v^b.v);return a.p+temp*a.v;}}l[N],q[N];bool Onleft(Line a,Point b){Point u=b-a.p;return (a.v^u)>=0;}bool HPI(int n){int i,st,en;sort(l+1,l+n+1);q[st=en=1]=l[1];for(i=2;i<=n;i++){while(st<en&&!Onleft(l[i],p[en-1]))en--;while(st<en&&!Onleft(l[i],p[st]))st++;if(fabs(l[i].v^q[en].v)<EPS)q[en]=Onleft(q[en],l[i].p)?l[i]:q[en];elseq[++en]=l[i];if(st<en)p[en-1]=Cross(q[en-1],q[en]);}while(st<en&&!Onleft(q[st],p[en-1]))en--;return en-st>=2;}int main(){int c,T,n,m,i,j,k;c=0;while(scanf("%d",&n)&&n){for(i=1;i<=n;i++)a[i].read();for(i=1;i<=n;i++)l[i].build(a[i+1>n?1:i+1],a[i]);printf("Floor #%d\n",++c);if(HPI(n))puts("Surveillance is possible.");elseputs("Surveillance is impossible.");putchar('\n');}return 0;}





1 0