首页 > 代码库 > 半平面交模板
半平面交模板
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 3392 | Accepted: 1497 |
Description
The first problem is to choose where to install the camera for every floor. The only requirement is that every part of the room must be visible from there. In the following figure the left floor can be completely surveyed from the position indicated by a dot, while for the right floor, there is no such position, the given position failing to see the lower left part of the floor.
Input
A zero value for n indicates the end of the input.
Output
Print a blank line after each test case.
Sample Input
40 00 11 11 080 00 21 21 12 12 23 23 00
Sample Output
Floor #1Surveillance is possible.Floor #2Surveillance is impossible.
#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>#include <cmath>using namespace std;#define EPS 1e-8#define N 1010int n;int dq[N];int top,bot,pn;int dump(double x){ if(fabs(x)<EPS) return 0; return x>0?1:-1;}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); } Point operator + (Point p){ return Point(x+p.x,y+p.y); } Point operator * (double d){ return Point(x*d,y*d); } double operator ^ (Point p){ return x*p.y-y*p.x; }};struct Line{ Point s,e; double k; Line(){} Line(Point s,Point e):s(s),e(e){ k=atan2(e.y-s.y,e.x-s.x); } Point operator & (Line l){ return s+(e-s)*(((l.e-l.s)^(l.s-s))/((l.e-l.s)^(e-s))); }};Line l[N];Point p[N],q[N];bool HPIcmp(Line a,Line b){ int d=dump(a.k-b.k); if(!d) return dump((b.s-a.s)^(b.e-a.s))>0; return d<0;}bool Judge(Line a,Line b,Line c){ Point p=b&c; return dump((a.s-p)^(a.e-p))<0;}void HPI(){ int i,j; sort(l,l+n,HPIcmp); for(i=0,j=0;i<n;i++) { if(dump(l[i].k-l[j].k)>0) l[++j]=l[i]; } n=j+1; dq[0]=0; dq[1]=1; top=1; bot=0; for(i=2;i<n;i++) { while(top>bot && Judge(l[i],l[dq[top]],l[dq[top-1]])) top--; while(top>bot && Judge(l[i],l[dq[bot]],l[dq[bot+1]])) bot++; dq[++top]=i; } while(top>bot && Judge(l[dq[bot]],l[dq[top]],l[dq[top-1]])) top--; while(top>bot && Judge(l[dq[top]],l[dq[bot]],l[dq[bot+1]])) bot++; if(top-bot>=2) cout<<"Surveillance is possible.\n\n"; else cout<<"Surveillance is impossible.\n\n";}int main(){ int T,iCase=1; while(scanf("%d",&n),n) { for(int i=0;i<n;i++) { scanf("%lf%lf",&q[i].x,&q[i].y); } for(int i=0;i<n;i++) { l[i]=Line(q[i],q[(i-1+n)%n]); } printf("Floor #%d\n",iCase++); HPI(); } return 0;}
POJ 1279
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 5600 | Accepted: 2372 |
Description
Input
Output
Sample Input
170 04 44 79 713 -18 -64 -4
Sample Output
80.00
#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>#include <cmath>using namespace std;#define EPS 1e-8#define N 1010int n;int dq[N];int top,bot,pn;int dump(double x){ if(fabs(x)<EPS) return 0; return x>0?1:-1;}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); } Point operator + (Point p){ return Point(x+p.x,y+p.y); } Point operator * (double d){ return Point(x*d,y*d); } double operator ^ (Point p){ return x*p.y-y*p.x; }};struct Line{ Point s,e; double k; Line(){} Line(Point s,Point e):s(s),e(e){ k=atan2(e.y-s.y,e.x-s.x); } Point operator & (Line l){ return s+(e-s)*(((l.e-l.s)^(l.s-s))/((l.e-l.s)^(e-s))); }};Line l[N];Point p[N],q[N];bool HPIcmp(Line a,Line b){ int d=dump(a.k-b.k); if(!d) return dump((b.s-a.s)^(b.e-a.s))>0; return d<0;}bool Judge(Line a,Line b,Line c){ Point p=b&c; return dump((a.s-p)^(a.e-p))<0;}void HPI(){ int i,j; sort(l,l+n,HPIcmp); for(i=0,j=0;i<n;i++) { if(dump(l[i].k-l[j].k)>0) l[++j]=l[i]; } n=j+1; dq[0]=0; dq[1]=1; top=1; bot=0; for(i=2;i<n;i++) { while(top>bot && Judge(l[i],l[dq[top]],l[dq[top-1]])) top--; while(top>bot && Judge(l[i],l[dq[bot]],l[dq[bot+1]])) bot++; dq[++top]=i; } while(top>bot && Judge(l[dq[bot]],l[dq[top]],l[dq[top-1]])) top--; while(top>bot && Judge(l[dq[top]],l[dq[bot]],l[dq[bot+1]])) bot++; dq[++top]=dq[bot]; for(pn=0,i=bot;i<top;i++,pn++) { p[pn]=l[dq[i+1]]&l[dq[i]]; }}double GetArea(){ double marea=0; if(pn<3) return 0; for(int i=2;i<pn;i++) { marea+=(p[i]-p[0])^(p[i-1]-p[0]); } return fabs(marea)/2;}int main(){ int T; scanf("%d",&T); while(T--) { scanf("%d",&n); for(int i=0;i<n;i++) { scanf("%lf%lf",&q[i].x,&q[i].y); } for(int i=0;i<n;i++) { l[i]=Line(q[i],q[(i-1+n)%n]); } HPI(); printf("%.2f\n",GetArea()); } return 0;}
POJ 3130
Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 3219 | Accepted: 1723 |
Description
After counting so many stars in the sky in his childhood, Isaac, now an astronomer and a mathematician uses a big astronomical telescope and lets his image processing program count stars. The hardest part of the program is to judge if shining object in the sky is really a star. As a mathematician, the only way he knows is to apply a mathematical definition of stars.
The mathematical definition of a star shape is as follows: A planar shape F is star-shaped if and only if there is a point C ∈ F such that, for any point P ∈ F, the line segment CP is contained in F. Such a point C is called a center of F. To get accustomed to the definition let’s see some examples below.
The first two are what you would normally call stars. According to the above definition, however, all shapes in the first row are star-shaped. The two in the second row are not. For each star shape, a center is indicated with a dot. Note that a star shape in general has infinitely many centers. Fore Example, for the third quadrangular shape, all points in it are centers.
Your job is to write a program that tells whether a given polygonal shape is star-shaped or not.
Input
The input is a sequence of datasets followed by a line containing a single zero. Each dataset specifies a polygon, and is formatted as follows.
n x1 y1 x2 y2 …
xn yn
The first line is the number of vertices, n, which satisfies 4 ≤ n ≤ 50. Subsequent n lines are the x- and y-coordinates of the n vertices. They are integers and satisfy 0 ≤ xi ≤ 10000 and 0 ≤ yi ≤ 10000 (i = 1, …, n). Line segments (xi, yi)–(xi + 1, yi + 1) (i = 1, …, n − 1) and the line segment (xn, yn)–(x1, y1) form the border of the polygon in the counterclockwise order. That is, these line segments see the inside of the polygon in the left of their directions.
You may assume that the polygon is simple, that is, its border never crosses or touches itself. You may assume assume that no three edges of the polygon meet at a single point even when they are infinitely extended.
Output
For each dataset, output “1
” if the polygon is star-shaped and “0
” otherwise. Each number must be in a separate line and the line should not contain any other characters.
Sample Input
6 66 13 96 61 76 98 13 94 4 0 45 68 8 27 21 55 14 93 12 56 95 15 48 38 46 51 65 64 31 0
Sample Output
10
#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>#include <cmath>using namespace std;#define EPS 1e-8#define N 1010int n;int dq[N];int top,bot,pn;int dump(double x){ if(fabs(x)<EPS) return 0; return x>0?1:-1;}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); } Point operator + (Point p){ return Point(x+p.x,y+p.y); } Point operator * (double d){ return Point(x*d,y*d); } double operator ^ (Point p){ return x*p.y-y*p.x; }};struct Line{ Point s,e; double k; Line(){} Line(Point s,Point e):s(s),e(e){ k=atan2(e.y-s.y,e.x-s.x); } Point operator & (Line l){ return s+(e-s)*(((l.e-l.s)^(l.s-s))/((l.e-l.s)^(e-s))); }};Line l[N];Point p[N],q[N];bool HPIcmp(Line a,Line b){ int d=dump(a.k-b.k); if(!d) return dump((b.s-a.s)^(b.e-a.s))>0; return d<0;}bool Judge(Line a,Line b,Line c){ Point p=b&c; return dump((a.s-p)^(a.e-p))>0;}void HPI(){ int i,j; sort(l,l+n,HPIcmp); for(i=0,j=0;i<n;i++) { if(dump(l[i].k-l[j].k)>0) l[++j]=l[i]; } n=j+1; dq[0]=0; dq[1]=1; top=1; bot=0; for(i=2;i<n;i++) { while(top>bot && Judge(l[i],l[dq[top]],l[dq[top-1]])) top--; while(top>bot && Judge(l[i],l[dq[bot]],l[dq[bot+1]])) bot++; dq[++top]=i; } while(top>bot && Judge(l[dq[bot]],l[dq[top]],l[dq[top-1]])) top--; while(top>bot && Judge(l[dq[top]],l[dq[bot]],l[dq[bot+1]])) bot++; if(top-bot>=2) cout<<"1\n"; else cout<<"0\n";}int main(){ int T; while(scanf("%d",&n),n) { for(int i=0;i<n;i++) { scanf("%lf%lf",&q[i].x,&q[i].y); } for(int i=0;i<n;i++) { l[i]=Line(q[i],q[(i-1+n)%n]); } HPI(); } return 0;}
POJ 3335
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 5439 | Accepted: 2166 |
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
#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>#include <cmath>using namespace std;#define EPS 1e-8#define N 1010int n;int dq[N];int top,bot,pn;int dump(double x){ if(fabs(x)<EPS) return 0; return x>0?1:-1;}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); } Point operator + (Point p){ return Point(x+p.x,y+p.y); } Point operator * (double d){ return Point(x*d,y*d); } double operator ^ (Point p){ return x*p.y-y*p.x; }};struct Line{ Point s,e; double k; Line(){} Line(Point s,Point e):s(s),e(e){ k=atan2(e.y-s.y,e.x-s.x); } Point operator & (Line l){ return s+(e-s)*(((l.e-l.s)^(l.s-s))/((l.e-l.s)^(e-s))); }};Line l[N];Point p[N],q[N];bool HPIcmp(Line a,Line b){ int d=dump(a.k-b.k); if(!d) return dump((b.s-a.s)^(b.e-a.s))>0; return d<0;}bool Judge(Line a,Line b,Line c){ Point p=b&c; return dump((a.s-p)^(a.e-p))<0;}void HPI(){ int i,j; sort(l,l+n,HPIcmp); for(i=0,j=0;i<n;i++) { if(dump(l[i].k-l[j].k)>0) l[++j]=l[i]; } n=j+1; dq[0]=0; dq[1]=1; top=1; bot=0; for(i=2;i<n;i++) { while(top>bot && Judge(l[i],l[dq[top]],l[dq[top-1]])) top--; while(top>bot && Judge(l[i],l[dq[bot]],l[dq[bot+1]])) bot++; dq[++top]=i; } while(top>bot && Judge(l[dq[bot]],l[dq[top]],l[dq[top-1]])) top--; while(top>bot && Judge(l[dq[top]],l[dq[bot]],l[dq[bot+1]])) bot++; top-bot>=2?cout<<"YES\n":cout<<"NO\n";}int main(){ int T; scanf("%d",&T); while(T--) { scanf("%d",&n); for(int i=0;i<n;i++) { scanf("%lf%lf",&q[i].x,&q[i].y); } for(int i=0;i<n;i++) { l[i]=Line(q[i],q[(i-1+n)%n]); } HPI(); } return 0;}
半平面交模板