zoukankan      html  css  js  c++  java
  • 【POJ 3335】 Rotating Scoreboard (多边形的核-

    Rotating Scoreboard

    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 nx1y1x2y2 ... xnyn where n (3 ≤ n ≤ 100) is the number of vertices in the polygon, and the pair of integers xiyi 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
    

    Source

     
     
    【分析】
      一个比较好的多边形的核的求法的解答:http://www.cnblogs.com/ka200812/archive/2012/01/20/2328316.html
     
      把原题转换成半平面的交,看看是否为空集。
      可以参考一下这个图:
      
    圈起来的是原多边形的顶点,其他是辅助线和辅助点,阴影部分是多边形的核。
     
      1 #include<cstdio>
      2 #include<cstdlib>
      3 #include<cstring>
      4 #include<iostream>
      5 #include<algorithm>
      6 #include<cmath>
      7 using namespace std;
      8 #define Maxn 100010
      9 
     10 const double eps=0.0001;
     11 const double pi=3.141592653;
     12 
     13 struct P {double x,y;};
     14 struct L {P a,b;double slop;}l[Maxn],p[Maxn];
     15 int len;
     16 
     17 double Dot(P x,P y) {return x.x*y.x+x.y*y.y;}
     18 double Cross(P x,P y) {return x.x*y.y-x.y*y.x;}
     19 
     20 P operator - (P x,P y)
     21 {
     22     P tt;
     23     tt.x=x.x-y.x;
     24     tt.y=x.y-y.y;
     25     return tt;
     26 }
     27 
     28 P operator + (P x,P y)
     29 {
     30     P tt;
     31     tt.x=x.x+y.x;
     32     tt.y=x.y+y.y;
     33     return tt;
     34 }
     35 
     36 P operator * (P x,double y)
     37 {
     38     P tt;
     39     tt.x=x.x*y;
     40     tt.y=x.y*y;
     41     return tt;
     42 }
     43 
     44 bool operator < (L x,L y) {return (x.slop==y.slop)?(Cross(x.b-x.a,y.b-x.a)<0):(x.slop<y.slop);}
     45 
     46 P inter(L x,L y)
     47 {
     48     P nw=y.a-x.a;
     49     P X=x.b-x.a,Y=y.b-y.a;
     50     double tt;
     51     tt=Cross(nw,X)/Cross(X,Y);
     52     return y.a+Y*tt;
     53 }
     54 
     55 bool jud(L x,L y,L z)
     56 {
     57     // if(x.slop==-y.slop) return 1;
     58     // if(x.slop-pi-y.slop<=eps&&x.slop-pi-y.slop>=-eps) return 1;
     59     // if(y.slop-pi-x.slop<=eps&&y.slop-pi-x.slop>=-eps) return 1;
     60     //???????
     61     P nw=inter(x,y);
     62     return Cross(z.b-z.a,nw-z.a)<0;
     63 }
     64 
     65 int cnt;
     66 
     67 void op()
     68 {
     69     for(int i=1;i<=cnt;i++)
     70     {
     71         printf("%.2lf %.2lf %.2lf %.2lf = %.2lf
    ",l[i].a.x,l[i].a.y,l[i].b.x,l[i].b.y,l[i].slop);
     72     }
     73     printf("
    ");
     74 }
     75 
     76 void opp(int L,int R)
     77 {
     78     for(int i=L;i<=R;i++)
     79     {
     80         printf("%.2lf %.2lf %.2lf %.2lf = %.2lf
    ",p[i].a.x,p[i].a.y,p[i].b.x,p[i].b.y,p[i].slop);
     81     }
     82     printf("
    ");
     83 }
     84 
     85 void ffind()
     86 {
     87     sort(l+1,l+1+cnt);
     88     // op();
     89     int tot=1;
     90     for(int i=2;i<=cnt;i++)
     91     {
     92         if(l[i].slop!=l[tot].slop) l[++tot]=l[i];
     93     }
     94     cnt=tot;
     95     if(cnt<=2) {printf("NO
    ");return;}
     96     // op();
     97     p[1]=l[1];p[2]=l[2];
     98     int L=1,R=2;
     99     for(int i=3;i<=cnt;i++)
    100     {
    101         while(R>L&&jud(p[R],p[R-1],l[i])) R--;
    102         while(R>L&&jud(p[L],p[L-1],l[i])) L++; 
    103         p[++R]=l[i];
    104     }
    105     // if(R>L&&jud(p[L],p[L+1],p[R])<0) R--;
    106     if(L<R&&jud(p[R-1],p[R],p[L])) R--;
    107     // opp(L,R);
    108     if(R-L+1<=2) printf("NO
    ");
    109     else printf("YES
    ");
    110 }
    111 
    112 int main()
    113 {
    114     int T;
    115     scanf("%d",&T);
    116     while(T--)
    117     {
    118         int n;
    119         scanf("%d",&n);
    120         P now,ft;
    121         scanf("%lf%lf",&ft.x,&ft.y);
    122         now=ft;
    123         cnt=0;
    124         for(int i=2;i<=n;i++)
    125         {
    126             P nw;
    127             scanf("%lf%lf",&nw.x,&nw.y);
    128             l[++cnt].a=nw;l[cnt].b=now;
    129             now=nw;
    130         }
    131         l[++cnt].a=ft;l[cnt].b=now;
    132         for(int i=1;i<=cnt;i++) l[i].slop=atan2(l[i].b.x-l[i].a.x,l[i].b.y-l[i].a.y);
    133         // op();
    134         ffind();
    135     }
    136     return 0;
    137 }
    View Code

    2016-12-26 18:51:45

  • 相关阅读:
    PHP 文件上传下载
    php文件类型MIME对照表
    如何书写安全的PHP代码
    wordpress顶部空白解决方案
    PHP发送邮件
    UTF8下面截取中文字符。
    dedecms 5.5 实现tag分页伪静态
    PHP MySQL 函数
    php异步调用
    归并排序及序列逆序数
  • 原文地址:https://www.cnblogs.com/Konjakmoyu/p/6223388.html
Copyright © 2011-2022 走看看