您的位置:首页 > 其它

poj 1474 Video Surveillance 半平面交

2016-11-10 22:37 351 查看
Video Surveillance

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 3963 Accepted: 1766
Description

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.

Source

Southwestern European Regional Contest 1997

#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;

#define all(x) (x).begin(), (x).end()
#define for0(a, n) for (int (a) = 0; (a) < (n); (a)++)
#define for1(a, n) for (int (a) = 1; (a) <= (n); (a)++)
#define mes(a,x,s) memset(a,x,(s)*sizeof a[0])
#define mem(a,x) memset(a,x,sizeof a)
#define ysk(x) (1<<(x))
typedef long long ll;
typedef pair<int, int> pii;
const int INF =0x3f3f3f3f;
const double inf =1e15;
const int maxn=100 ;

const double PI=cos(-1.0);
const double eps=1e-10;
int n;
int dcmp(double x)
{
if(fabs(x)<eps) return 0;
else return x<0?-1:1;
}
struct Point
{
double x,y;
Point(double x=0,double y=0):x(x),y(y) {};
bool operator ==(const Point B)const {return dcmp(x-B.x)==0&&dcmp(y-B.y)==0;}

bool operator<(const Point& b)const
{
return dcmp(x-b.x)<0|| dcmp(x-b.x)==0 &&dcmp(y-b.y)<0;
}
};
typedef Point Vector;
Vector operator -(Vector A,Vector B) {return Vector(A.x-B.x,A.y-B.y); }

double Cross(Vector A,Vector B)//叉乘
{
return A.x*B.y-A.y*B.x;
}

double Dot(Vector A,Vector B)//点乘
{
return 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 p) {return Vector(A.x*p,A.y*p); }
Vector operator /(Vector A,double p) {return Vector(A.x/p,A.y/p); }
Vector operator -(Vector A) {return Vector(-A.x,-A.y);}

struct Line
{
Point p,p2;
Vector v;
double ang;
Line(){}
Line(Point a,Vector v):p(a),v(v){ang=atan2(v.y,v.x); }//点线式
void twoPointIntial(Point p,Point p2)//两点式
{
this->p=p;
this->p2=p2;
v= p2-p;
ang=atan2(v.y,v.x);
}

bool operator<(const Line & L)const
{
return ang<L.ang;
}
};
Point GetLineIntersection(const Line& a, const Line& b)
{
Vector u = a.p-b.p;
double t = Cross(b.v, u) / Cross(a.v, b.v);
return a.p+a.v*t;
}

bool OnLeft(Line L,Point p)
{
return dcmp(Cross(L.v,p-L.p))>=0;
}

vector<Point> HalfplaneIntersection(vector<Line>& L) {
int n = L.size();
sort(L.begin(), L.end()); // 按极角排序

int first, last; // 双端队列的第一个元素和最后一个元素的下标
vector<Point> p(n); // p[i]为q[i]和q[i+1]的交点
vector<Line> q(n); // 双端队列
vector<Point> ans; // 结果

q[first=last=0] = L[0]; // 双端队列初始化为只有一个半平面L[0]
for(int i = 1; i < n; i++) {
while(first < last && !OnLeft(L[i], p[last-1])) last--;
while(first < last && !OnLeft(L[i], p[first])) first++;
q[++last] = L[i];
if(fabs(Cross(q[last].v, q[last-1].v)) < eps) { // 两向量平行且同向,取内侧的一个
last--;
if(OnLeft(q[last], L[i].p)) q[last] = L[i];
}
if(first < last) p[last-1] = GetLineIntersection(q[last-1], q[last]);
}
while(first < last && !OnLeft(q[first], p[last-1])) last--; // 删除无用平面
if(last - first <= 1) return ans; // 空集
p[last] = GetLineIntersection(q[last], q[first]); // 计算首尾两个半平面的交点

// 从deque复制到输出中
for(int i = first; i <= last; i++) ans.push_back(p[i]);
return ans;
}

vector<Line> L;
vector<Point> ans;

Point poly[maxn+10];
int main()
{
std::ios::sync_with_stdio(false);
int kase=0;
while(cin>>n&&n)
{
L.clear();
double x,y;
for0(i,n) cin>>poly[i].x>>poly[i].y;
reverse(poly,poly+n);
for(int i=0;i<n;i++)
{
int y=(i+1)%n;
L.push_back(Line( poly[i] ,poly[y]-poly[i]) );
}
Line tmp;
// tmp.twoPointIntial(Point(-inf,-inf),Point(inf,-inf));;
// L.push_back(tmp );加不加边框均对
//
// tmp.twoPointIntial(Point(inf,-inf),Point(inf,inf));
// L.push_back(tmp);
//
// tmp.twoPointIntial(Point(inf,inf),Point(-inf,inf));
// L.push_back(tmp );
//
// tmp.twoPointIntial(Point(-inf,inf),Point(-inf,-inf));
// L.push_back(tmp );

// cout<<HalfplaneIntersection(L,n,poly)<<endl;
printf("Floor #%d\n",++kase);
ans=HalfplaneIntersection(L);
puts(ans.size() ?"Surveillance is possible.\n":"Surveillance is impossible.\n");

}
return 0;
}

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