poj 3335 Rotating Scoreboard (半平面交)

Rotating Scoreboard
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 6815 Accepted: 2706

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

[Submit]   [Go Back]   [Status]   [Discuss]


题目大意:求多边形是否有内核。

题解:半平面交

什么是多边形的内核?

它是平面简单多边形的核是该多边形内部的一个点集,该点集中任意一点与多边形边界上一点的连线都处于这个多边形内部。就是一个在一个房子里面放一个摄像 头,能将所有的地方监视到的放摄像头的地点的集合即为多边形的核。

 

         

如上图,第一个图是有内核的,比如那个黑点,而第二个图就不存在内核了,无论点在哪里,总有地区是看不到的。

那么我们可以用半平面交来解决这个问题,用多边形相邻两个顶点确定的直线切割平面,每次取直线的一侧,同左或同右。

这道题卡精度,叉积求交点的方法过不了。。。。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<vector>
#define N 203
#define eps 1e-8
using namespace std;
struct Vector{
	double x,y;
	Vector (double X=0,double Y=0){
		x=X,y=Y;
	}
}a[N],p[N],tmp[N];
int n,m;
double inf=1e18;
typedef Vector point;
Vector operator -(Vector a,Vector b){return Vector (a.x-b.x,a.y-b.y);}
Vector operator +(Vector a,Vector b){return Vector (a.x+b.x,a.y+b.y);}
Vector operator *(Vector a,double t){return Vector (a.x*t,a.y*t);}
Vector operator /(Vector a,double t) {
	return Vector (a.x/t,a.y/t);
}
bool operator ==(Vector a,Vector b){return a.x==b.x&&a.y==b.y;}
bool operator !=(Vector a,Vector b) {
	return a.x!=b.x||a.y!=b.y;
}
int dcmp(double x)
{
	if (fabs(x)<eps) return 0;
	return x<0?-1:1;
}
double cross(Vector a,Vector b)
{
	return a.x*b.y-a.y*b.x;
}
point line_intersection(point a,point a0,point b,point b0)
{
    double a1,b1,c1,a2,b2,c2;
    a1 = a.y - a0.y;
    b1 = a0.x - a.x;
    c1 = cross(a,a0);
    a2 = b.y - b0.y;
    b2 = b0.x - b.x;
    c2 = cross(b,b0);
    double d = a1 * b2 - a2 * b1;
    return point((b1 * c2 - b2 * c1) / d,(c1 * a2 - c2 * a1) / d);
} 
double dot(Vector a,Vector b){
	return a.x*b.x+a.y*b.y;
}
void init()
{
	m=0;
	p[m++]=point(inf,inf);
	p[m++]=point(-inf,inf);
	p[m++]=point(-inf,-inf);
	p[m++]=point(inf,-inf);
}
void cut(point a,point b)
{
	int dn=0;
	double t1,t2;
	for (int i=0;i<m;i++){
		t1=cross((b-a),(p[i]-a));
		t2=cross((b-a),(p[(i+1)%m]-a));
		if (dcmp(t1)>=0) tmp[dn++]=p[i];
		if (dcmp(t1)*dcmp(t2)<0) 
		 tmp[dn++]=line_intersection(a,b,p[i],p[(i+1)%m]);
	}
	m=0;
	for (int i=0;i<dn;i++)
	  p[m++]=tmp[i];
}
int main()
{
	freopen("a.in","r",stdin);
	int T;
	scanf("%d",&T);
	while (T--){
		scanf("%d",&n);
		memset(a,0,sizeof(a));
		for (int i=1;i<=n;i++) scanf("%lf%lf",&a[i].x,&a[i].y);
		init();
		a[n+1]=a[1];
		for (int i=2;i<=n+1;i++){
		 cut(a[i],a[i-1]);
		 if (!m) break;
	    }
		if (m) printf("YES\n");
		else printf("NO\n");
	}
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值