Enumeration + Judging segment intersection
#include <cstdio>#include<cmath>#include<cstring>#include<cmath>#include<algorithm>#include<map>#include<vector>using namespacestd;Const intinf=0x7FFFFFFF;Const intmaxn= -+Ten;#defineEPS 1e-8intN,ans,num;Doublepx,py;structline{DoubleSx,sy; DoubleEx,ey;} L[MAXN];structpoint{Doublex; Doubley; Point (DoubleADoubleb) {x=A; Y=b; }};DoubleCross_pro (Point p0, point P1, point p2) {return(p1.x-p0.x) * (P2.Y-P0.Y)-(p2.x-p0.x) * (p1.y-p0.y);}intDBLCMP (Doubled) { if(Fabs (d) <EPS)return 0; return(D >0) ?1: -1;}BOOLIs_intersect (Point P1, point P2, point P3, point P4) {returnDBLCMP (Cross_pro (P3, P4, p1)) * DBLCMP (Cross_pro (P3, P4, p2)) = =-1;}intMain () { while(~SCANF ("%d",&N)) {ans=INF; for(intI=1; i<=n; i++) scanf ("%LF%LF%LF%LF", &l[i]. Sx,&l[i]. Sy,&l[i]. ex,&L[i]. Ey); scanf ("%LF%LF",&px,&Py); for(intI=1; i<=n; i++) {point A (L[i]. Sx,l[i]. Sy); Point B (Px,py); Num=0; for(intj=1; j<=n; J + +) {point C (l[j]. SX,L[J]. Sy); Point D (L[j]. EX,L[J]. Ey); if(Is_intersect (a,b,c,d)) num++; } ans=min (ans,num); Point AA (L[i]. Ex,l[i]. Ey); Point BB (Px,py); Num=0; for(intj=1; j<=n; J + +) {point C (l[j]. SX,L[J]. Sy); Point D (L[j]. EX,L[J]. Ey); if(Is_intersect (aa,bb,c,d)) num++; } ans=min (ans,num); } if(n==0) printf ("Number of doors = 1\n"); Elseprintf"Number of doors =%d\n", ans+1); } return 0;}
POJ 1066 Treasure Hunt