poj 3335 Rotating Scoreboard(多边形的核存在性)

来源:互联网 发布:送巧克力知乎 编辑:程序博客网 时间:2024/06/06 04:44
Rotating Scoreboard
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 3652 Accepted: 1392

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

24 0 0 0 1 1 1 1 08 0 0  0 2  1 2  1 1  2 1  2 2  3 2  3 0

Sample Output

YESNO

Source

Tehran 2006 Preliminary

题目:http://poj.org/problem?id=3335

题意:给你一个多边形,判断是否存在一个区域使得多边形每条边上的点都能这个区域

分析:典型的多边形核的存在性判断,每条边看做一个半平面,然后做半平面交即可,这题很简单,不过为了写半平面交的模板,搞了我好久,最后还是失败了,很多地方都写错了,最终拿了个别人的模板,改成自己的风格,实现一个算法还是不容易的啊= =

代码:

#include<cmath>#include<cstdio>#include<iostream>#include<algorithm>using namespace std;const int mm=111;typedef double DIY;struct point{    DIY x,y;    point(){}    point(DIY _x,DIY _y):x(_x),y(_y){}}g[mm];point MakeVector(point &P,point &Q){    return point(Q.x-P.x,Q.y-P.y);}DIY CrossProduct(point P,point Q){    return P.x*Q.y-P.y*Q.x;}DIY MultiCross(point P,point Q,point R){    return CrossProduct(MakeVector(Q,P),MakeVector(Q,R));}struct halfPlane{    point s,t;    double angle;    halfPlane(){}    halfPlane(point _s,point _t):s(_s),t(_t){}    halfPlane(DIY sx,DIY sy,DIY tx,DIY ty):s(sx,sy),t(tx,ty){}    void GetAngle()    {        angle=atan2(t.y-s.y,t.x-s.x);    }}hp[mm],q[mm];point IntersectPoint(halfPlane P,halfPlane Q){    DIY a1=CrossProduct(MakeVector(P.s,Q.t),MakeVector(P.s,Q.s));    DIY a2=CrossProduct(MakeVector(P.t,Q.s),MakeVector(P.t,Q.t));    return point((P.s.x*a2+P.t.x*a1)/(a2+a1),(P.s.y*a2+P.t.y*a1)/(a2+a1));}bool cmp(halfPlane P,halfPlane Q){    if(fabs(P.angle-Q.angle)<1e-8)        return MultiCross(P.s,P.t,Q.s)>0;    return P.angle<Q.angle;}bool IsParallel(halfPlane P,halfPlane Q){    return fabs(CrossProduct(MakeVector(P.s,P.t),MakeVector(Q.s,Q.t)))<1e-8;}void HalfPlaneIntersect(int n,int &m){    sort(hp,hp+n,cmp);    int i,l=0,r=1;    for(m=i=1;i<n;++i)        if(hp[i].angle-hp[i-1].angle>1e-8)hp[m++]=hp[i];    n=m;    m=0;    q[0]=hp[0],q[1]=hp[1];    for(i=2;i<n;++i)    {        if(IsParallel(q[r],q[r-1])||IsParallel(q[l],q[l+1]))return;        while(l<r&&MultiCross(hp[i].s,hp[i].t,IntersectPoint(q[r],q[r-1]))>0)--r;        while(l<r&&MultiCross(hp[i].s,hp[i].t,IntersectPoint(q[l],q[l+1]))>0)++l;        q[++r]=hp[i];    }    while(l<r&&MultiCross(q[l].s,q[l].t,IntersectPoint(q[r],q[r-1]))>0)--r;    while(l<r&&MultiCross(q[r].s,q[r].t,IntersectPoint(q[l],q[l+1]))>0)++l;    q[++r]=q[l];    for(i=l;i<r;++i)        g[m++]=IntersectPoint(q[i],q[i+1]);}int main(){    int i,n,m,t;    scanf("%d",&t);    while(t--)    {        scanf("%d",&n);        for(i=0;i<n;++i)            scanf("%lf%lf",&g[i].x,&g[i].y);        g[n]=g[0];        for(i=0;i<n;++i)        {            hp[i]=halfPlane(g[i+1],g[i]);            hp[i].GetAngle();        }        HalfPlaneIntersect(n,m);        puts(m>2?"YES":"NO");    }    return 0;}


原创粉丝点击