poj1474 Video Surveillance
题意:求多边形的内核,即:在多边形内部找到某个点,使得从这个点能不受阻碍地看到多边形的所有位置.
只要能看到所有的边,就能看到所有的位置.那么如果我们能够在多边形的内部的点x看到某条边AB,这个点x一定在AB的”内侧”,如果按逆时针方向给出多边形的所有顶点并假设从A到B是逆时针行走,”内侧”就是指有向直线A->B的左侧,那么多边形的每条边对应了一个半平面,要想看见这条边必须保证x在这个半平面内.而且,只要对于每条边x都在这条边的左侧,那么x就是一个可行的点,能够看到整个多边形.
这个结论的必要性是显然的(如果x在某条边的右侧,那么一定看不到这条边)
充分性也可以证:假如存在某个点是x看不见的,那么从x向这个点连线,一定会穿过偶数条多边形的边(因为每穿过一条边就从多边形的内部走到外部或者从外部走到内部,而起点终点都在内部),那么这些边一定是有一半从边的左侧穿到右侧(即从内到外),一半从边的右侧穿到左侧(即从外到内).也就是说,如果存在一个x看不见的点,就一定存在一条多边形的边使得x在这条边的右侧,于是如果找不到一条多边形的边使得x在这条边的右侧,就不存在x看不见的点,于是这个结论具有充分性.
那么我们对n条边求一下半平面交,判段是否非空即可.注意这里结果是一个点或一个线段时也判作有解,我的方法是把多边形的每条边都向多边形外部沿法向量方向平移一点,这样点和线段就都变成了面积不为0的区域,避免了边界讨论.
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
const int maxn=;
const double eps=1e-;
int cmp(double x){return x<-eps?-:x>eps;}
struct point{
double x,y;point(){}
point(double a,double b){x=a;y=b;}
void read(){scanf("%lf%lf",&x,&y);}
}P[maxn],p[maxn];
point operator +(point a,point b){return point(a.x+b.x,a.y+b.y);}
point operator -(point a,point b){return point(a.x-b.x,a.y-b.y);}
double cross(point a,point b){return a.x*b.y-a.y*b.x;}
struct line{
point s,d;double arg;
bool operator <(const line &B)const{return cmp(arg-B.arg)==-;}
line(){}
line(point a,point b){s=a;d=b;arg=atan2(d.y,d.x);}
void output(){
printf("(%f,%f)+(%f,%f)\n",s.x,s.y,d.x,d.y);
}
}L[maxn],q[maxn];
int n;
bool onleft(line A,point B){
return cmp(cross(A.d,B-A.s))>;
}
point mult(double t,point A){
return point(t*A.x,t*A.y);
}
point intersect(line A,line B){
double t=cross(B.d,A.s-B.s)/cross(A.d,B.d);
return A.s+mult(t,A.d);
}
point rot(point A,double arg){
return point(A.x*cos(arg)-A.y*sin(arg),A.x*sin(arg)+A.y*cos(arg));
}
int HPI(){
sort(L,L+n);
int head,tail;head=tail=;q[tail++]=L[];
for(int i=;i<n;++i){
while(head+<tail&&!onleft(L[i],p[tail-]))tail--;
while(head+<tail&&!onleft(L[i],p[head])) head++;
q[tail++]=L[i];
if(head+<tail&&cmp(cross(q[tail-].d,q[tail-].d))==){
tail--;
if(onleft(q[tail-],L[i].s))q[tail-]=L[i];
}
if(head+<tail)p[tail-]=intersect(q[tail-],q[tail-]);
}
while(head+<tail&&!onleft(q[head],p[tail-]))tail--;
// q[head].output();q[head+1].output();
if(tail-head<=)return ;
else return ;
}
point normal(point A){return point(-A.y,A.x);}
point operator *(const double &t,const point &A){return point(A.x*t,A.y*t);}
int main(){
int tests=;
while(scanf("%d",&n),n!=){
for(int i=;i<n;++i)P[i].read();P[n]=P[];
for(int i=;i<n;++i)L[i]=line(P[i]-eps*normal(P[i]-P[i+]),P[i]-P[i+]);
if(HPI())printf("Floor #%d\nSurveillance is possible.\n",++tests);
else printf("Floor #%d\nSurveillance is impossible.\n",++tests);
printf("\n");
}
return ;
}
poj1474 Video Surveillance的更多相关文章
- POJ1474 Video Surveillance(半平面交)
求多边形核的存在性,过了这题但是过不了另一题的,不知道是模板的问题还是什么,但是这个模板还是可以过绝大部分的题的... #pragma warning(disable:4996) #include & ...
- POJ1474:Video Surveillance(求多边形的核)(占位)
A friend of yours has taken the job of security officer at the Star-Buy Company, a famous depart- me ...
- poj 1474 Video Surveillance - 求多边形有没有核
/* poj 1474 Video Surveillance - 求多边形有没有核 */ #include <stdio.h> #include<math.h> const d ...
- poj 1474 Video Surveillance (半平面交)
链接:http://poj.org/problem?id=1474 Video Surveillance Time Limit: 1000MS Memory Limit: 10000K Total ...
- POJ1474:Video Surveillance——题解
http://poj.org/problem?id=1474 题目大意:给按照顺时针序的多边形顶点,问其是否有内核. —————————————————————————————— (和上道题目一模一样 ...
- POJ 1474 Video Surveillance(半平面交)
题目链接 2Y,模版抄错了一点. #include <cstdio> #include <cstring> #include <string> #include & ...
- Video Surveillance - POJ 1474(判断是否存在内核)
题目大意:询问是否在家里装一个监视器就可以监控所有的角落. 分析:赤裸裸的判断多边形内核题目. 代码如下: #include<iostream> #include<string.h& ...
- ●poj 1474 Video Surveillance
题链: http://poj.org/problem?id=1474 题解: 计算几何,半平面交 半平面交裸题,快要恶心死我啦... (了无数次之后,一怒之下把onleft改为onright,然后还加 ...
- POJ - 1474 :Video Surveillance (半平面交-求核)
pro:顺时针给定多边形,问是否可以放一个监控,可以监控到所有地方,即问是否存在多边形的核. 此题如果两点在同一边界上(且没有被隔段),也可以相互看到. sol:求多边形是否有核.先给直线按角度排序, ...
随机推荐
- codechef Table Game(博弈)
题意 题目链接 很难概括.. Sol (因为比赛还没结束,所以下面讲的可能是“非官方”“正解”) maya这题我前前后后 断断续续的做了一个星期才A掉.CC一场challenge出两道打表题可有点过分 ...
- Docker - 容器中的tomcat如何使用startup.sh启动
网上大多介绍的catalina.sh启动,因为docker容器中,无法直接启动startup.sh. 解决方法: 编辑catalina.sh,找到 >> "$CATALINA_O ...
- 转 Cobar使用文档(可用作MySQL大型集群解决方案)
转自:http://blog.csdn.net/shagoo/article/details/8191346 最近好不容易抽空研究了下Cobar,感觉这个产品确实很不错(在文档方面比Amoeba强多了 ...
- hdu2509Be the Winner(反nim博弈)
Be the Winner Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- 180606-Linux下jdk中文乱码问题解决
文章链接:https://liuyueyi.github.io/hexblog/2018/06/06/180606-Linux下jdk中文乱码问题解决/ linux下jdk中文乱码问题解决 之前遇到过 ...
- 使用unittest里面的discover()方法组织测试用例
import osimport unittest directory = os.getcwd()# 测试用例的目录organize = unittest.defaultTestLoader.disco ...
- lesson 15 Fifty pence worth of trouble
lesson 15 Fifty pence worth of trouble appreciate =be grateful for We really appreciate all the help ...
- 158. Valid Anagram【LintCode by java】
Description Write a method anagram(s,t) to decide if two strings are anagrams or not. Clarification ...
- 理解 JavaScript 原型 / 原型链
关于对象 以下代码中 p 的值是一个新对象,里面拥有 name 和 age 属性 function People(name, age){ this.name = name this.age = age ...
- 用 splice 函数分别实现 push、pop、shift、unshift 的方法
主要需要注意的是不同方法他们本身返回的值应该是什么,是数组当前的长度,还是取出的元素的值,再在splice函数里面进行相应的return就可以了.具体如下: 用 splice函数实现 push方法 f ...