poj1474——半平面交(顺时针)

题目链接:http://poj.org/problem?id=1474

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.

题目翻译:

你的一个朋友在Star-Buy公司担任了保安,一家著名的离别店。他的任务之一是安装一个视频监控系统,以确保客户的安全(当然,商品的安全)在商店的所有楼层。由于公司的预算有限,每层楼只能有一台摄像机。但是这些摄像机可能会转向向各个方向看。‎
‎第一个问题是选择在每个楼层安装摄像机‎
‎的位置。唯一的要求是房间的每个部分都必须从那里可见。在下图中,左地板可以从点指示的位置完全测量,而对于右侧,没有这样的位置,给定的位置看不到地板的左下部。‎

‎在尝试安装摄像机之前,您的朋友首先想知道是否有适合他们的位置。因此,他要求你写一个程序,假设一个地面计划,去胺是否有一个位置,从中整个地板是可见的。所有地面平面都形成矩形多边形,其边缘不相互相交,仅在拐角处相互接触。‎

‎输入‎

‎输入包含多个楼层说明。每个描述都从绑定地板的顶点数 n 开始(4 <= n <= 100)。下一个 n 行包含两个整数,即 n 顶点的 x 和 y 坐标,按顺时针顺序给出。所有顶点将是不同的,并且位于多边形的角上。因此,边缘在水平和垂直之间交替。‎
‎ n 的零值表示输入‎
‎的结束。‎

‎输出‎

‎对于每个测试用例,首先输出一条包含地板数的线,如样本输出所示。然后打印一行,说明"监视是可能的。"如果存在可以观察整个楼层的位置,或者打印"监视是不可能的",如果没有这样的位置。‎
‎在每个测试用例后打印一张空白‎
‎行。

 

经典半平面交例题,输入数据顺序为顺时针,具体过程可以参考这个题(poj3335——半平面交模板(方向不定)

#include<iostream>
#include<cmath> 
#include<algorithm>
using namespace std;
const double eps = 1e-8;
const int inf=0x3f3f3f3f;
struct Point{
    double x, y;
    Point(double x = 0, double y = 0):x(x),y(y){}
}p[1005];
int n;
typedef Point Vector;
Vector operator + (Vector A, Vector B){
    return Vector(A.x+B.x, A.y+B.y);
}
Vector operator - (Point A, Point 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);
}
int sgn(double x){
    if(fabs(x) < eps)
        return 0;
    if(x < 0)
        return -1;
    return 1;
}
double Dot(Vector A, Vector B){
    return A.x*B.x + A.y*B.y;
}
double Cross(Vector A, Vector B){
    return A.x*B.y-A.y*B.x;
}
double Length(Vector A){
    return sqrt(Dot(A, A));
}
Vector Normal(Vector A){//向量A左转90°的单位法向量
    double L = Length(A);
    return Vector(-A.y/L, A.x/L);
}
struct Line{
    Point p;//直线上任意一点
    Vector v;//方向向量,它的左边就是对应的半平面
    double ang;//极角,即从x轴正半轴旋转到向量v所需要的角(弧度)
    Line(){}
    Line(Point p, Vector v) : p(p), v(v){
        ang = atan2(v.y, v.x);
    }
    bool operator < (const Line& L) const {//排序用的比较运算符
        return ang < L.ang;
    }
}l[1005];
//点p在有向直线L的左侧
bool OnLeft(Line L, Point p){
    return Cross(L.v, p - L.p) >= 0;
}
//两直线交点。假定交点唯一存在
Point GetIntersection(Line a, 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;
}
//半平面交的主过程
int HalfplaneIntersection(Line* L, int n, Point* poly){
    sort(L, L + n);//按照极角排序
    int fst = 0, lst = 0;//双端队列的第一个元素和最后一个元素
    Point *P = new Point[n];//p[i] 为 q[i]与q[i + 1]的交点
    Line *q = new Line[n];//双端队列
    q[fst = lst = 0] = L[0];//初始化为只有一个半平面L[0]
    for(int i = 1; i < n; ++i){
        while(fst < lst && !OnLeft(L[i], P[lst - 1])) --lst;
        while(fst < lst && !OnLeft(L[i], P[fst])) ++fst;
        q[++lst] = L[i];
        if(sgn(Cross(q[lst].v, q[lst - 1].v)) == 0){
            //两向量平行且同向,取内侧一个
            --lst;
            if(OnLeft(q[lst], L[i].p)) q[lst] = L[i];
        }
        if(fst < lst)
            P[lst - 1] = GetIntersection(q[lst - 1], q[lst]);
    }
    while(fst < lst && !OnLeft(q[fst], P[lst - 1])) --lst;
    //删除无用平面
    if(lst - fst <= 1) return 0;//空集
    P[lst] = GetIntersection(q[lst], q[fst]);//计算首尾两个半平面的交点
    //从deque复制到输出中
    int m = 0;
    for(int i = fst; i <= lst; ++i) poly[m++] = P[i];
    return m;
}
int main(){
	int kcase=0; 
	while(~scanf("%d",&n)&&n){
		for(int i=0;i<n;i++)
			scanf("%lf%lf",&p[i].x,&p[i].y);
			l[0]=Line(p[0],p[n-1]-p[0]); 
		for(int i=1;i<n;i++)
			l[i]=Line(p[i],p[i-1]-p[i]);
		int res=HalfplaneIntersection(l,n,p);
		printf("Floor #%d\n",++kcase);
		if(!res) printf("Surveillance is impossible.\n");
		else printf("Surveillance is possible.\n");
		printf("\n");
	}
	return 0;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值