bsoj 1850 【POJ1474】監控攝像頭

Input

輸入文件有多組數據。每一組的第一行是一個整數n(4<=n<=100),表示房間的示意圖是一個n邊形。接下來n行,按照順時針給出這n個點的座標。最後一組的第一行是0,表示結束。

Output

如果能找出安放攝像機的點則輸出
Floor #1
Surveillance is possible.
否則輸出:
Floor #1
Surveillance is impossible.
#後邊的數字表示房間編號,按輸入數據由小到大編號,第一個房間編號爲1。每一組數據完後輸出一個空行。

Sample Input

40 00 11 11 080 00 21 21 12 12 23 23 00

Sample Output

Floor #1
Surveillance is possible.

Floor #2
Surveillance is impossible.
這個題不知道爲什麼,在windows上FC和輸出文件一模一樣,在Linux測評機上就是過不去。
也是半平面交,只不過有一點變化。
如果是凸多邊形就一定可行,不管用它。
主要是凹多邊形。
用自己的邊去做半平面,割自己的面積,割完後只要還有點(而不是面積)就可以。
我用的是一個超大的矩形作平面,這樣據說要快一點。
如圖,割出來後就不行了。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
struct Point {
	double x,y;
}P[100005],last,cur,first;
int n,m,T;
double Cross(Point a,Point b,Point c){
	return (a.x-c.x)*(b.y-c.y)-(b.x-c.x)*(a.y-c.y);
}
Point Get_NewPoint(Point a,Point b,Point l,Point r){
	Point Temp;
	double x=abs(Cross(a,l,r));
	double y=abs(Cross(b,l,r));
	Temp.x=(a.x*y+b.x*x)/(x+y);
	Temp.y=(a.y*y+b.y*x)/(x+y);
	return Temp;
}
void Cut(Point x,Point y){
	Point Temp[50];
	int top=0;
	P[n+1]=P[1];
	double l=Cross(P[1],y,x);
	for(int i=2;i<=n+1;i++){
		double r=Cross(P[i],y,x);
		if(l>=0){
			Temp[++top]=P[i-1];
			if(r<0)Temp[++top]=Get_NewPoint(P[i-1],P[i],x,y);
		}
		else if(r>0)Temp[++top]=Get_NewPoint(P[i-1],P[i],x,y);
		l=r;
	}
	for(int i=1;i<=top;i++)P[i]=Temp[i];
	n=top;
}
void Get_Ans(){
	P[n+1]=P[1];
	double Ans=0.0;
	for(int i=2;i<=n+1;i++){
		Ans+=(P[i-1].x-P[i].x)*(P[i-1].y+P[i].y)/2.0;
	}
	printf("%.3lf",abs(Ans));
}
int main(){
	T=0;
	while(scanf("%d",&m)==1&&m){
	T++;
	n=4;
	P[1].x=-1e8,P[1].y-1e8;
	P[2].x=-1e8,P[2].y=1e8;
	P[3].x=1e8,P[3].y=1e8;
	P[4].x=1e8,P[4].y=-1e8;
	scanf("%lf%lf",&first.x,&first.y);
	last=first;
	for(int i=2;i<=m;i++){
		
		scanf("%lf%lf",&cur.x,&cur.y);
		Cut(last,cur);
		last=cur;
	}
	Cut(last,first);
	printf("Floor #%d\n",T);
	if(n)printf("Surveillance is possible.\n\n");
	else printf("Surveillance is impossible.\n\n") ;
	}
	return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章