您的位置:首页 > 其它

zoj 1248 || poj 1474 Video Surveillance(判断多边形是否存在核)

2011-07-10 17:49 639 查看
判断一个多边形是否有核。
跟UVA10078不同的是,10078是判断是否存在一个点看不到另一个存在多边形的点,所以判断它是否为凸包就好了。而这个是判断多边形是否存在核。
开始想错了,用面积判断呢,WA了,看discuss说是用核的点的个数判断,如果个数为0那么就不存在核,恩,其实刚开始也有这么想,不过不太确定。。。还是对半平面交这个东东理解不好啊。。。所以上个代码改改就好了。。

#include <queue>
#include <stack>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <limits.h>
#include <string.h>
#include <string>
#include <algorithm>

using namespace std;

const int MAX = 105;
struct point{ double x,y;};
point p[MAX],s[MAX];
const double eps = 1e-6;
bool dy(double x,double y)	{	return x > y + eps;}	// x > y 
bool xy(double x,double y)	{	return x < y - eps;}	// x < y 
bool dyd(double x,double y)	{ 	return x > y - eps;}	// x >= y 
bool xyd(double x,double y)	{	return x < y + eps;} 	// x <= y 
bool dd(double x,double y) 	{	return fabs( x - y ) < eps;}  // x == y
double crossProduct(point a,point b,point c)//向量 ac 在 ab 的方向 
{
	return (c.x - a.x)*(b.y - a.y) - (b.x - a.x)*(c.y - a.y);
}
point l2l_inst_p(point u1,point u2,point v1,point v2)
{
	point ans = u1;
	double t = ((u1.x - v1.x)*(v1.y - v2.y) - (u1.y - v1.y)*(v1.x - v2.x))/
				((u1.x - u2.x)*(v1.y - v2.y) - (u1.y - u2.y)*(v1.x - v2.x));
	ans.x += (u2.x - u1.x)*t;
	ans.y += (u2.y - u1.y)*t;
	return ans;
}
void inst_hp(point p[],int n,point s[],int &len)
{
	point tp[MAX];	
	p
 = p[0];
	for(int i=0; i<=n; i++)
		tp[i] = p[i];
	int cp = n,tc;
	for(int i=0; i<n; i++)
	{
		tc = 0;
		for(int k=0; k<cp; k++)
		{
			if( dyd(crossProduct(p[i],p[i+1],tp[k]),0.0) )// 顺时针的话是dyd 
				s[tc++] = tp[k];
			if( xy(crossProduct(p[i],p[i+1],tp[k])* 
					crossProduct(p[i],p[i+1],tp[k+1]),0.0) )
				s[tc++] = l2l_inst_p(p[i],p[i+1],tp[k],tp[k+1]);
		}
		s[tc] = s[0];
		for(int k=0; k<=tc; k++)
			tp[k] = s[k];
		cp = tc;
	}
	len = cp;
}

int main()
{
	int len,ncases,n,ind = 1;
	while( ~scanf("%d",&n) && n )
	{
		for(int i=0; i<n; i++)
			scanf("%lf%lf",&p[i].x,&p[i].y);
		
		inst_hp(p,n,s,len);
		
		printf("Floor #%d\n",ind++);
		if( len )
			printf("Surveillance is possible.\n\n");
		else
			printf("Surveillance is impossible.\n\n");
	}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: