Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 4963 | Accepted: 1980 |
Description
This year, ACM/ICPC World finals will be held in a hall in form of a simple polygon. The coaches and spectators are seated along the edges of the polygon. We want to place a rotating scoreboard somewhere in the hall such that a spectator sitting anywhere on the boundary of the hall can view the scoreboard (i.e., his line of sight is not blocked by a wall). Note that if the line of sight of a spectator is tangent to the polygon boundary (either in a vertex or in an edge), he can still view the scoreboard. You may view spectator's seats as points along the boundary of the simple polygon, and consider the scoreboard as a point as well. Your program is given the corners of the hall (the vertices of the polygon), and must check if there is a location for the scoreboard (a point inside the polygon) such that the scoreboard can be viewed from any point on the edges of the polygon.
Input
The first number in the input line, T is the number of test cases. Each test case is specified on a single line of input in the form n x1 y1 x2 y2 ... xn yn where n (3 ≤ n ≤ 100) is the number of vertices in the polygon, and the pair of integers xi yi sequence specify the vertices of the polygon sorted in order.
Output
The output contains T lines, each corresponding to an input test case in that order. The output line contains either YES or NO depending on whether the scoreboard can be placed inside the hall conforming to the problem conditions.
Sample Input
2 4 0 0 0 1 1 1 1 0 8 0 0 0 2 1 2 1 1 2 1 2 2 3 2 3 0
Sample Output
YES NO //AC代码
/* 参考博客:http://blog.csdn.net/accry/article/details/6070621 此博客对半平面方面知识讲得很全面,通俗 题意:利用半平面交求多边形的核 多边形的核: 它是平面简单多边形的核是该多边形内部的一个点集 该点集中任意一点与多边形边界上一点的连线都处于这个多边形内部。 就是一个在一个房子里面放一个摄像头 能将所有的地方监视到的放摄像头的地点的集合即为多边形的核 经常会遇到让你判定一个多边形是否有核的问题 */ #include<iostream> #include<queue> #include<cstdio> #include<algorithm> #include<cstring> #include<iomanip> #include<map> #include<cstdlib> #include<cmath> const int INF=0x7fffffff; const double eps=1e-8; const double PI=acos(-1.0); const int Max=1001; #define zero(x) (((x)>0?(x):-(x))<eps) #define mm(a,b) memset(a,b,sizeof(a)) using namespace std; int sign(double x) { return (x>eps)-(x<-eps); } typedef struct Node { double x; double y; }point; point list[Max],stack[Max]; point qq[Max],pp[Max],pnt[Max]; int n; int top; int cnt; int curcnt; double xmult(point p0,point p1,point p2) { return(p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y); } double Distance(point p1,point p2)// 返回两点之间欧氏距离 { return( sqrt( (p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y) ) ); } bool cmp(point p1,point p2) { double temp; temp=xmult(list[0],p1,p2); if(temp>0) return true; if(temp==0&&(Distance(list[0],p1)<Distance(list[0],p2))) return true; return false; } void convex_hull()//凸包模板 { int i; for(i=1;i<n;i++) { point temp; if((list[i].y<list[0].y)||(list[i].y==list[0].y&&list[i].x<list[0].x)) swap(list[0],list[i]); } sort(list+1,list+n,cmp); top=1; stack[0]=list[0]; stack[1]=list[1]; for(i=2;i<n;i++) { while(top>=1&&xmult(stack[top-1],stack[top],list[i])<=0) top--; top++; stack[top]=list[i]; } } void Init(int n) { int i; for(i=1;i<=n;i++) { pp[i]=pnt[i]; } pp[n+1]=pnt[1];//n个点有n条边 pp[0]=pnt[n];//用于计算相交的点(第n个点在直线左侧并且第一个点在右侧) cnt=n;//多边形顶点数 } void GetLine(point u,point v,double &a,double &b,double &c)//两点确定一条直线 { a=v.y-u.y; b=u.x-v.x; c=v.x*u.y-u.x*v.y; } point Intersect(point u,point v,double a,double b,double c)//求直线切割交于多边形边上的一点 { double q=fabs(a*u.x+b*u.y+c); double p=fabs(a*v.x+b*v.y+c); point res; res.x=((p*u.x+q*v.x)/(q+p)); res.y=((p*u.y+q*v.y)/(q+p)); return res; } void CutLine(double a,double b,double c)//直线切割模板 { int i; curcnt=0; for(i=1;i<=cnt;i++) { if(a*pp[i].x+b*pp[i].y+c>=0)//当前顶点在直线的右侧(或者直线上) { qq[++curcnt]=pp[i]; } else { if(a*pp[i-1].x+b*pp[i-1].y+c>0)//前一个顶点在直线的右侧 { qq[++curcnt]=Intersect(pp[i],pp[i-1],a,b,c); } if(a*pp[i+1].x+b*pp[i+1].y+c>0)//同样的,后一个顶点在直线的右侧 { qq[++curcnt]=Intersect(pp[i],pp[i+1],a,b,c); } } } for(i=1;i<=curcnt;i++) { pp[i]=qq[i]; } pp[curcnt+1]=pp[1]; pp[0]=pp[curcnt]; cnt=curcnt; } int main() { int m,i,j; int T; double a,b,c; cin>>T; while(T--) { cin>>n; for(i=1;i<=n;i++) { cin>>pnt[i].x>>pnt[i].y; } pnt[n+1]=pnt[1];//n个点有n条边 Init(n); //顺时针方向为切割后多边形 for(i=1;i<=n;i++) { GetLine(pnt[i],pnt[i+1],a,b,c); CutLine(a,b,c); } if(cnt==0) cout<<"NO"<<endl; else cout<<"YES"<<endl; } return 0; }