码迷,mamicode.com
首页 > 其他好文 > 详细

poj-1474 Video Surveillance

时间:2015-08-12 19:21:45      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:poj   计算几何   半平面交   

题意:

判断多边形是否存在核;

点集顺时针或逆时针给出,n<=100;


题解:

半平面交模板题;

多边形的核就在组成多边形的半平面的交上;

也可以顺便说明多边形的核若存在则一定是凸的;

原因似乎画画图是比较显然的;

一个地方被挡住一定是因为那被另一条边挡住了嘛;


注意半平面交的判断点与直线位置关系要用>=号;

此题买一送二,我大胆地在提交框里改输出然后光荣的WA了= =

poj-1474 poj-3130 poj-3335


代码:


#include<math.h>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define N 110
using namespace std;
const double EPS=1e-10;
const double pi=acos(-1.0);
struct Point
{
	double x,y;
	void read()
	{
		scanf("%lf%lf",&x,&y);
	}
	Point operator +(Point a)
	{
		a.x=x+a.x,a.y=y+a.y;
		return a;
	}
	Point operator -(Point a)
	{
		a.x=x-a.x,a.y=y-a.y;
		return a;
	}
	double operator ^(Point a)
	{
		return x*a.y-y*a.x;
	}
	friend Point operator *(double a,Point b)
	{
		b.x=a*b.x,b.y=a*b.y;
		return b;
	}
}a[N],p[N];
struct Line
{
	Point p,v;
	double alpha;
	void build(Point a,Point b)
	{
		p=a,v=b-a;
		alpha=atan2(v.y,v.x);
	}
	friend bool operator <(Line a,Line b)
	{
		return a.alpha<b.alpha;
	}
	friend Point Cross(Line a,Line b)
	{
		Point u=a.p-b.p;
		double temp=(b.v^u)/(a.v^b.v);
		return a.p+temp*a.v;
	}
}l[N],q[N];
bool Onleft(Line a,Point b)
{
	Point u=b-a.p;
	return (a.v^u)>=0;
}
bool HPI(int n)
{
	int i,st,en;
	sort(l+1,l+n+1);
	q[st=en=1]=l[1];
	for(i=2;i<=n;i++)
	{
		while(st<en&&!Onleft(l[i],p[en-1]))
			en--;
		while(st<en&&!Onleft(l[i],p[st]))
			st++;
		if(fabs(l[i].v^q[en].v)<EPS)
			q[en]=Onleft(q[en],l[i].p)?l[i]:q[en];
		else
			q[++en]=l[i];
		if(st<en)
			p[en-1]=Cross(q[en-1],q[en]);
	}
	while(st<en&&!Onleft(q[st],p[en-1]))
		en--;
	return en-st>=2;
}
int main()
{
	int c,T,n,m,i,j,k;
	c=0;
	while(scanf("%d",&n)&&n)
	{
		for(i=1;i<=n;i++)
			a[i].read();
		for(i=1;i<=n;i++)
			l[i].build(a[i+1>n?1:i+1],a[i]);
		printf("Floor #%d\n",++c);
		if(HPI(n))
			puts("Surveillance is possible.");
		else
			puts("Surveillance is impossible.");
		putchar('\n');
	}
	return 0;
}







poj-1474 Video Surveillance

标签:poj   计算几何   半平面交   

原文地址:http://blog.csdn.net/ww140142/article/details/47448733

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!