POJ - 1474 Video Surveillance【半平面交裸题】

A friend of yours has taken the job of security officer at the Star-Buy Company, a famous depart- ment store. One of his tasks is to install a video surveillance system to guarantee the security of the customers (and the security of the merchandise of course) on all of the store’s countless floors. As the company has only a limited budget, there will be only one camera on every floor. But these cameras may turn around to look in every direction.
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.
在这里插入图片描述
Before trying to install the cameras, your friend first wants to know whether there is indeed a suitable position for them. He therefore asks you to write a program that, given a ground plan, de- termines whether there is a position from which the whole floor is visible. All floor ground plans form rectangular polygons, whose edges do not intersect each other and touch each other only at the corners.

Input
The input contains several floor descriptions. Every description starts with the number n of vertices that bound the floor (4 <= n <= 100). The next n lines contain two integers each, the x and y coordinates for the n vertices, given in clockwise order. All vertices will be distinct and at corners of the polygon. Thus the edges alternate between horizontal and vertical.
A zero value for n indicates the end of the input.

Output
For every test case first output a line with the number of the floor, as shown in the sample output. Then print a line stating “Surveillance is possible.” if there exists a position from which the entire floor can be observed, or print “Surveillance is impossible.” if there is no such position.
Print a blank line after each test case.

Sample Input
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
0

Sample Output
Floor #1
Surveillance is possible.

Floor #2
Surveillance is impossible.

#include<iostream>
#include<algorithm>
#include<cstdio> 
#include<cstring>
#include<cmath>
#define maxn 2010
#define eps 1e-8
using namespace std;
int n,m,tol,bot,top; //bot和top指向双端队列的底部和顶部 
int dq[maxn],order[maxn]; //双端队列和排序后的…… 
struct point{ //点 
	double x,y;
}p[maxn];
struct vct{ //向量(直线)
	point start,end;
	double angle; //极角 
}v[maxn];
int dbcmp(double k){ //判断为0,还是为正负 
	if(fabs(k)<eps) return 0;
	return k>0?1:-1;
}
//用叉积判断点在直线的哪一侧,因为点是逆时针排序的,>0则在直线左侧,<0则在直线右侧 毋庸置疑 
double Multi(point p0,point p1,point p2){ //向量 p0p1 和 p0p2 相乘 
	return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
}
int cmp(int x,int y){ //排序的比较函数 
	int d=dbcmp(v[x].angle-v[y].angle);
	//极角相同,越靠左侧的排在越前面,方便后面操作 
	if(!d) return dbcmp(Multi(v[x].start,v[y].start,v[y].end))>0;  
	//大于0取向量左半部分为半平面,小于0,取右半部分
	return d<0; //按角度排序,角度小的在前 
}
void addline(double x1,double y1,double x2,double y2){ //增加一个向量(直线) 
	v[tol].start.x=x1; v[tol].start.y=y1;
	v[tol].end.x=x2;   v[tol].end.y=y2;
	//atan2是求原点到(x,y)的直线与x轴的夹角,比atan稳定 
	v[tol].angle=atan2(y2-y1,x2-x1);
	order[tol]=tol; tol++;
}
void GetIntersect(vct v1,vct v2,point &p){ //获取交点并返回给p 
	double dot1,dot2;
	dot1=Multi(v1.start,v2.end,v1.end);
	dot2=Multi(v1.start,v2.start,v1.end);
	p.x=(dot1*v2.start.x-dot2*v2.end.x)/(dot1-dot2);		
	p.y=(dot1*v2.start.y-dot2*v2.end.y)/(dot1-dot2);
}
int Judge(vct v0,vct v1,vct v2){ //判断 v1 和 v2 的交点 pt 在半平面 v0内 
	point pt;
	GetIntersect(v1,v2,pt);
	return dbcmp(Multi(pt,v0.start,v0.end))<0;
	//叉积大于0 pt在 v0 的左面,小于0 pt 在 v0 的右面
	//如果 pt 在半平面 v0 左侧则返回真 
}
void HalfPlaneIntersection(){ //求解半平面交 
	int i,j;
	sort(order,order+tol,cmp); //极角排序 
	for(i=1,j=0;i<tol;i++) //排除极角相同的 
		if(dbcmp(v[order[i]].angle-v[order[j]].angle)>eps) order[++j]=order[i]; 
	tol=j+1; //个数 
	dq[0]=order[0]; //开始时入队两条直线 
	dq[1]=order[1];
	bot=0; top=1;
	for(i=2;i<tol;i++){ //如果双端队列底端或顶端两个半平面的交点在当前半平面之外,则删除 
		while(bot<top&&Judge(v[order[i]],v[dq[top-1]],v[dq[top]])) top--;
		while(bot<top&&Judge(v[order[i]],v[dq[bot+1]],v[dq[bot]])) bot++;
		dq[++top]=order[i]; //将当前半平面加入队列的顶端 
	}
	//处理一下头和尾 
	while(bot<top&&Judge(v[dq[bot]],v[dq[top-1]],v[dq[top]])) top--;
	while(bot<top&&Judge(v[dq[top]],v[dq[bot+1]],v[dq[bot]])) bot++;
	m=0;
    if(top<=bot+1) return;
    for(i=bot;i<top;i++) GetIntersect(v[dq[i]],v[dq[i+1]],p[m++]);
    if(top>bot+1) GetIntersect(v[dq[bot]],v[dq[top]],p[m++]);//即最终的线段大于2条,可形成环时	
}
int main(){
	int n,cas=0;
	while(scanf("%d",&n)&&n){
		for(int i=0;i<n;i++) scanf("%lf%lf",&p[i].x,&p[i].y);
		reverse(p,p+n);
		tol=0;
		for(int i=0;i<n-1;i++) addline(p[i].x,p[i].y,p[i+1].x,p[i+1].y);
		addline(p[n-1].x,p[n-1].y,p[0].x,p[0].y);
		HalfPlaneIntersection();
		if(top-bot>1){
			printf("Floor #%d\n",++cas);
			printf("Surveillance is possible.\n");
		}
		else{
			printf("Floor #%d\n",++cas);
			printf("Surveillance is impossible.\n");
		}
		printf("\n");
	}
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值