您的位置:首页 > 其它

POJ 3130: How I Mathematician Wonder What You Are! 半平面交

2018-02-11 21:09 573 查看
How I Mathematician Wonder What You Are!
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 4073 Accepted: 2194
DescriptionAfter 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.
InputThe 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 
x1y1
x2y2
xnyn
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.
OutputFor 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 Input6
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
0Sample Output1
0SourceJapan 2006
这是一道优美的半平面交裸题
写的过程简直是对蒟蒻计算几何功力的升华

#include<cmath>
#include<ctime>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<iomanip>
#include<vector>
#include<string>
#include<bitset>
#include<queue>
#include<set>
#include<map>
using namespace std;

typedef double db;

inline int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch<='9'&&ch>='0'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
void print(int x)
{if(x<0)putchar('-'),x=-x;if(x>=10)print(x/10);putchar(x%10+'0');}

const int N=60;
const db eps=1e-8;

struct point
{
db x,y;

friend point operator +(const point &a,const point &b)
{return (point){a.x+b.x,a.y+b.y};}

friend point operator -(const point &a,const point &b)
{return (point){a.x-b.x,a.y-b.y};}

friend db cross(const point &x,const point &y,const point &bas)
{
point a(x-bas),b(y-bas);
return a.x*b.y-a.y*b.x;
}
}p
;

int scnt;
struct segment
{
point X,Y;db angle;

friend bool operator <(const segment &a,const segment &b)
{
if(abs(a.angle-b.angle)<eps) return cross(a.Y,a.X,b.X)>0;
return a.angle<b.angle;
}

friend point get_node(const segment &a,const segment &b)
{
point res;
db s[2];
s[0]=cross(a.X,a.Y,b.X);
s[1]=cross(a.Y,a.X,b.Y);
res.x=(b.Y.x*s[0]+b.X.x*s[1])/(s[0]+s[1]);
res.y=(b.Y.y*s[0]+b.X.y*s[1])/(s[0]+s[1]);
return res;
}

friend bool check(const segment &a,const segment &b,const segment &c)
{
point tmp=get_node(a,b);
return cross(c.X,c.Y,tmp)<eps;
}
}seg
;

inline void add_seg(const point &a,const point &b)
{
seg[++scnt].X=a,seg[scnt].Y=b;
seg[scnt].angle=atan2(b.y-a.y,b.x-a.x);
}

int n;

int q
;

int hpi()
{
sort(seg+1,seg+1+n);
register int i,tot(1),head(0),tail(2);
for(i=2;i<=n;++i)
if(seg[i].angle-seg[tot].angle>eps)
seg[++tot]=seg[i];
q[0]=1;q[1]=2;
for(i=3;i<=tot;++i)
{
while(head<tail-1 && check(seg[q[tail-1]],seg[q[tail-2]],seg[i])) tail--;
while(head<tail-1 && check(seg[q[head]],seg[q[head+1]],seg[i])) head++;
q[tail++]=i;
}
while(head<tail-1 && check(seg[q[tail-1]],seg[q[tail-2]],seg[q[head]])) tail--;
while(head<tail-1 && check(seg[q[head]],seg[q[head+1]],seg[q[tail-1]])) head++;
return tail-head;
}

int main()
{
register int i;
while(1)
{
scnt=0;
n=read();
if(!n) break;
for(i=1;i<=n;++i)
p[i].x=read(),p[i].y=read();
p[n+1]=p[1];
for(i=1;i<=n;++i)
add_seg(p[i],p[i+1]);
hpi()>2 ? puts("1") : puts("0");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐