1 //segment intersection POJ 10662 //idea: Direct enumeration of each endpoint and end point into a line segment, judging the number of intersections with the remaining segments3 4 //#include <bits/stdc++.h>5#include <iostream>6#include <cstdio>7#include <cstdlib>8#include <algorithm>9#include <vector>Ten#include <math.h> One using namespacestd; A #defineLL Long Long -typedef pair<int,int>PII; - Const intINF =0x3f3f3f3f; the ConstLL MOD =100000000LL; - Const intN = the; - #defineCLC (A, B) memset (A,b,sizeof (a)) - Const DoubleEPS = 1e-8; + voidFre () {freopen ("In.txt","R", stdin);} - voidFreout () {freopen ("OUT.txt","W", stdout);} +InlineintRead () {intx=0, f=1;CharCh=getchar (); while(ch>'9'|| ch<'0') {if(ch=='-') f=-1; Ch=getchar ();} while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; Ch=getchar ();}returnx*F;} A at intSgnDoublex) { - if(Fabs (x) < EPS)return 0; - if(X <0)return-1; - Else return 1; - } - structpoint{ in Doublex, y; - Point () {} toPoint (Double_x,Double_y) { +x = _x;y =_y; - } thePointoperator-(ConstPoint &b)Const{ * returnPoint (X-b.x,y-b.y); $ }Panax Notoginseng Double operator^(ConstPoint &b)Const{ - returnx*b.y-y*b.x; the } + Double operator*(ConstPoint &b)Const{ A returnx*b.x + y*b.y; the } + }; - $ structline{ $ Point s,e; - intInx; - Line () {} the Line (Point _s,point _e) { -S=_s;e=_e;Wuyi } the }; - Wu Line Line[n]; - BOOLInter (line L1,line L2) { About return $Max (l1.s.x,l1.e.x) >= min (l2.s.x,l2.e.x) && -Max (l2.s.x,l2.e.x) >= min (l1.s.x,l1.e.x) && -Max (l1.s.y,l1.e.y) >= min (l2.s.y,l2.e.y) && -Max (l2.s.y,l2.e.y) >= min (l1.s.y,l1.e.y) && ASGN ((l2.s-l1.s) ^ (L1.E-L1.S)) *SGN ((l2.e-l1.s) ^ (L1.E-L1.S)) <=0&& +SGN ((L1.S-L2.S) ^ (L2.E-L2.S)) *SGN ((L1.E-L2.S) ^ (L2.E-L2.S)) <=0; the } - $Vector<line>list; the BOOLCMP (line l1,line L2) { the returnl1.inx<L2.inx; the } the -Point p[ the]; in intMain () { the intN; the while(~SCANF ("%d",&N)) { About for(intI=1; i<=n;i++){ the DoubleX1,y1,x2,y2; thescanf"%LF%LF%LF%LF",&x1,&y1,&x2,&y2); theline[i]=Line (Point (X1,y1), point (X2,y2)); +p[i*2-1]=Point (x1,y1); -p[i*2]=Point (x2,y2); the }Bayi Doublex1,y1; thescanf"%LF%LF",&x1,&y1); thePoint s=Point (x1,y1); - intans=inf; - for(intI=1; i<=2*n;i++){ the inttem=0; the for(intj=1; j<=n;j++){ the if(Inter (line (S,p[i]), Line[j])) thetem++; - } theans=min (ans,tem); the } the 94 Point p1; theP1=point (0,0); the inttem=0; the for(intI=1; i<=n;i++){98 if(Inter (line (S,P1), Line[i])) Abouttem++; - }101Ans=min (ans,tem+1);102 103P1=point (0, -);104tem=0; the for(intI=1; i<=n;i++){106 if(Inter (line (S,P1), Line[i]))107tem++;108 }109Ans=min (ans,tem+1); the 111P1=point ( -,0); thetem=0;113 for(intI=1; i<=n;i++){ the if(Inter (line (S,P1), Line[i])) thetem++; the }117Ans=min (ans,tem+1);118 119P1=point ( -, -); -tem=0;121 for(intI=1; i<=n;i++){122 if(Inter (line (S,P1), Line[i]))123tem++;124 } theAns=min (ans,tem+1);126printf"Number of doors =");127printf"%d\n", ans); - }129 return 0; the}
Segment Intersection POJ 1066