poj 3335(半平面交)

链接:http://poj.org/problem?id=3335     //大牛们常说的测模板题

----------------------------------------------------------------

Rotating Scoreboard

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 5158   Accepted: 2061

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

Source

Tehran 2006 Preliminary

-----------------------------------------------------------------------------------

看了N久,感觉好难

分别看了这里:http://blog.csdn.net/dream_ysl/article/details/7831293

这里:http://blog.csdn.net/zxy_snow/article/details/6596237

这里:http://blog.csdn.net/accry/article/details/6070621

一直引用大牛zxy_snow的模板,希望大牛别生气啊

个人觉得半平面交确实要花好多功夫,

一要理解N^2的是求如何出来的                       //用给定的多边形每一条边去切割它自己

二要判断给定的点是逆时针or顺时针,要加判断    //用连向原点的面积正负判断

三要理解代码,每一次切割过后的边数要更新,给下一个循环

四要自己在草稿纸上画画,一定理解了才去做题(测模板)

-----------------------------------------------------------------------------------

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <stdlib.h>
 4 #include <iostream>
 5 #include <algorithm>
 6 #include <math.h>
 7
 8 using namespace std;
 9
10 #define eps 1e-8
11 #define MAXX 105
12 typedef struct
13 {
14     double x;
15     double y;
16 }point;
17
18 point p[MAXX],s[MAXX];
19
20 bool dy(double x,double y) {return x>y+eps; }
21 bool xy(double x,double y) {return x<y-eps; }
22 bool dyd(double x,double y){return x>y-eps; }
23 bool xyd(double x,double y){return x<y+eps; }
24 bool dd(double x,double y) {return fabs(x-y)<eps; }
25
26 double crossProduct(point a,point b,point c)
27 {
28     return (c.x-a.x)*(b.y-a.y)-(c.y-a.y)*(b.x-a.x);
29 }
30
31 point IntersectPoint(point u1,point u2,point v1,point v2)
32 {
33     point ans=u1;
34     double t=((u1.x-v1.x)*(v1.y-v2.y)-(u1.y-v1.y)*(v1.x-v2.x))/
35              ((u1.x-u2.x)*(v1.y-v2.y)-(u1.y-u2.y)*(v1.x-v2.x));
36     ans.x += (u2.x-u1.x)*t;
37     ans.y += (u2.y-u1.y)*t;
38     return ans;
39 }
40
41 void cut(point p[],point s[],int n,int &len)
42 {
43     point tp[MAXX];
44     p[n]=p[0];
45     for(int i=0; i<=n; i++)
46     {
47         tp[i]=p[i];
48     }
49     int cp=n,tc;
50     for(int i=0; i<n; i++)
51     {
52         tc=0;
53         for(int k=0; k<cp; k++)
54         {
55             if(dyd(crossProduct(p[i],p[i+1],tp[k]),0.0))
56                 s[tc++]=tp[k];
57             if(xy(crossProduct(p[i],p[i+1],tp[k])*
58                   crossProduct(p[i],p[i+1],tp[k+1]),0.0))
59                 s[tc++]=IntersectPoint(p[i],p[i+1],tp[k],tp[k+1]);
60         }
61         s[tc]=s[0];
62         for(int k=0; k<=tc; k++)
63             tp[k]=s[k];
64         cp=tc;
65     }
66     len=cp;
67 }
68
69 int main()
70 {
71     int n,m,i,j;
72     scanf("%d",&n);
73     while(n--)
74     {
75         scanf("%d",&m);
76         for(i=0; i<m; i++)
77             scanf("%lf%lf",&p[i].x,&p[i].y);
78             int len;
79             cut(p,s,m,len);
80         if(len)printf("YES\n");
81         else printf("NO\n");
82     }
83     return 0;
84 }

时间: 2024-08-06 16:05:03

poj 3335(半平面交)的相关文章

POJ 3335 半平面交求多边形的核

Rotating Scoreboard Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4899   Accepted: 1946 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 ed

poj 3335 /poj 3130/ poj 1474 半平面交 判断核是否存在 / poj1279 半平面交 求核的面积

1 /*************** 2 poj 3335 点序顺时针 3 ***************/ 4 #include <iostream> 5 #include <cmath> 6 #include <algorithm> 7 using namespace std; 8 const double eps = 1e-8; 9 const double maxn = 0x7f7f7f7f; 10 int dcmp(double x){ 11 if(fabs(

POJ 2540 半平面交求可行区域面积

Hotter Colder Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2343   Accepted: 981 Description The children's game Hotter Colder is played as follows. Player A leaves the room while player B hides an object somewhere in the room. Player

poj 1279 半平面交核面积

Art Gallery Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 6668   Accepted: 2725 Description The art galleries of the new and very futuristic building of the Center for Balkan Cooperation have the form of polygons (not necessarily conve

POJ 3525 /// 半平面交 模板

题目大意: 给定n,接下来n行逆时针给定小岛的n个顶点 输出岛内离海最远的点与海的距离 半平面交模板题 将整个小岛视为由许多半平面围成 那么以相同的比例缩小这些半平面 一直到缩小到一个点时 那个点就是离海最远的点 #include <cstdio> #include <cmath> #include <vector> #include <algorithm> using namespace std; const double eps=1e-10; doubl

POJ 3525 半平面交+二分

二分所能形成圆的最大距离,然后将每一条边都向内推进这个距离,最后所有边组合在一起判断时候存在内部点 1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 #include <algorithm> 5 #include <cmath> 6 7 using namespace std; 8 #define N 105 9 #define ll long long 10 #de

poj 3335 Rotating Scoreboard(半平面交)

Rotating Scoreboard Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6420   Accepted: 2550 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 ed

半平面交 模板 poj 3335 poj 3130 poj 1474 判断半平面交是否为空集

半平面交模板 const double pi= acos(-1.0); #define arc(x) (x / 180 * pi) const double EPS = 1e-8; const int Max_N = 105; struct Point{ double x,y; Point(){} Point(double x, double y):x(x),y(y){} Point operator - (Point p){ return Point(x- p.x , y - p.y ) ;

POJ 3335 Rotating Scoreboard(半平面交求多边形核)

题目链接 题意 : 给你一个多边形,问你在多边形内部是否存在这样的点,使得这个点能够看到任何在多边形边界上的点. 思路 : 半平面交求多边形内核. 半平面交资料 关于求多边形内核的算法 什么是多边形的内核? 它是平面简单多边形的核是该多边形内部的一个点集,该点集中任意一点与多边形边界上一点的连线都处于这个多边形内部.就是一个在一个房子里面放一个摄像 头,能将所有的地方监视到的放摄像头的地点的集合即为多边形的核. 如上图,第一个图是有内核的,比如那个黑点,而第二个图就不存在内核了,无论点在哪里,总