●poj 1474 Video Surveillance
题链:
http://poj.org/problem?id=1474
题解:
计算几何,半平面交
半平面交裸题,快要恶心死我啦。。。
(了无数次之后,一怒之下把onleft改为onright,然后还加了一个去重,总算是过了。。。)
代码:
#include<cmath> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #define MAXN 1550 using namespace std; const double eps=1e-8; int sign(double x){ if(fabs(x)<=eps) return 0; return x<0?-1:1; } struct Point{ double x,y; Point(double _x=0,double _y=0):x(_x),y(_y){} void Read(){scanf("%lf%lf",&x,&y);} }D[MAXN]; typedef Point Vector; struct Line{ Point s; Vector v; double ang; Line(){} Line(Point _s,Vector _v):s(_s),v(_v){ang=atan2(v.y,v.x);} friend bool operator < (Line A,Line B){return sign(A.ang-B.ang)<0;} }L[MAXN]; bool operator == (Point A,Point B){return sign(A.x-B.x)==0&&sign(A.y-B.y)==0;} 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 k){return Vector(A.x*k,A.y*k);} double operator ^ (Vector A,Vector B){return A.x*B.y-A.y*B.x;} double operator * (Vector A,Vector B){return A.x*B.x+A.y*B.y;} bool OnRight(Point P,Line A){return sign(A.v^(P-A.s))<0;} Point LLI(Line A,Line B){//Line_Line_Intersection Vector u=B.s-A.s; double t1=(u^B.v)/(A.v^B.v); return A.s+A.v*t1; } int HPI(int lnt){//Half_Plane_Intersection static Point QP[MAXN]; static Line QL[MAXN]; sort(L+1,L+lnt+1); int l=1,r=1,dnt=0; QL[r]=L[1]; for(int i=2;i<=lnt;i++){ while(l<r&&OnRight(QP[r-1],L[i])) r--; while(l<r&&OnRight(QP[l],L[i])) l++; QL[++r]=L[i]; if(sign(QL[r-1].v*QL[r].v)>0&&sign(QL[r-1].v^QL[r].v)==0){ r--; if(!OnRight(L[i].s,QL[r])) QL[r]=L[i]; } if(l<r) QP[r-1]=LLI(QL[r-1],QL[r]); } while(l<r&&OnRight(QP[r-1],QL[l])) r--; if(r-l<=1) return 0; QP[r]=LLI(QL[r],QL[l]); for(int i=l;i<=r;i++) D[++dnt]=QP[i]; dnt=unique(D+1,D+dnt+1)-D-1; return dnt-(dnt>1&&D[dnt]==D[1]); } int main(){ int n,Case=0; while(~scanf("%d",&n)&&n){ for(int i=1;i<=n;i++) D[i].Read(),L[i]=Line(D[i],D[i-1]-D[i]); L[1]=Line(D[1],D[n]-D[1]); n=HPI(n); printf("Floor #%d\n",++Case); //printf("%d\n",n); if(n>0) printf("Surveillance is possible.\n"); else printf("Surveillance is impossible.\n"); printf("\n"); } return 0; }
Do not go gentle into that good night.
Rage, rage against the dying of the light.
————Dylan Thomas