POJ 3304 Segments
Test instructions: given n (n<=100) line segments, ask if you have such a line, so that all segments are projected down, at least one intersection.
Idea: For the intersecting shadow projected on the line above, we can make a line there, then this line and all segments have at least one intersection, so if there is a line and all segments have intersections, then there must be a solution.
How do you know if there are straight lines and all segments intersect? How do you enumerate such a line? The idea is to fix two points, which can be arbitrarily taken on all segments, and then use these two points as straight lines to judge other segments. Why is it? Because if there is a line and all segments intersect, then I can definitely pan to the end position of a certain limit, and then rotate to the end position of a certain limit, nor lose the positive solution. The point of the bug is that the two points of the enumeration are the emphasis, and the direction vector of the line is a 0 vector, which will be judged to intersect all the segments. ~~
#include <cstdio>#include<cstdlib>#include<cstring>#include<cmath>#include<algorithm>using namespacestd;#defineINF (0X3F3F3F3F)typedefLong Long intLL; #include<iostream>#include<sstream>#include<vector>#include<Set>#include<map>#include<queue>#include<string>Const intMAXN = the+ -;structcoor{Doublex, y; Coor () {} coor (DoubleXxDoubleyy): X (xx), Y (yy) {}Double operator^ (coor rhs)Const //Calculate cross product (vector product) { returnx*rhs.y-y*rhs.x; } cooroperator-(coor rhs)Const //subtract coordinates, a-B gets the vector ba { returnCoor (x-rhs.x,y-rhs.y); } Double operator* (coor rhs)Const //Quantity Product { returnX*rhs.x + y*Rhs.y; }};Const DoubleEPS = 1e-8;structline{coor point1,point2; Line () {} line (coor xx,coor yy):p oint1 (xx), Point2 (yy) {}BOOL operator& (line RHS)Const //determine if line and RHS segments intersect { //represent a straight line, but RHS represents a line segment//train of thought, determine whether the two endpoints on the RHS line are on the same side of this line, with one side, and do not intersectCoor ff1 = point2-point1;//direction vector of a line return(((RHS.POINT1-POINT1) ^ff1) * (((rhs.point2-point1) ^ff1)) <=0;//symbols are different or have 0, proving intersect}}A[MAXN];intN;BOOLSame (DoubleADoubleb) { returnFabs (A-B) <EPS;}BOOLcheck (coor aa,coor bb) {line T=Line (AA,BB); for(intI=1; i<=n;++i) {if(! (t&A[i])) { return false; } } return true;}voidWork () {scanf ("%d",&N); for(intI=1; i<=n;++i) {scanf ("%LF%LF%LF%LF",&a[i].point1.x,&a[i].point1.y,&a[i].point2.x,&a[i].point2.y); } if(n==1) {printf ("yes!\n"); return ; } for(intI=1; i<=n;++i) { for(intj=i+1; j<=n;++j) {if(check (a[i].point1,a[j].point1)) {printf ("yes!\n"); return ; } if(check (a[i].point1,a[j].point2)) {printf ("yes!\n"); return ; } if(check (a[i].point2,a[j].point1)) {printf ("yes!\n"); return ; } if(check (a[i].point2,a[j].point2)) {printf ("yes!\n"); return ; }}} printf ("no!\n"); return ;}intMain () {#ifdef local freopen ("Data.txt","R", stdin);#endif intT; scanf ("%d",&t); while(t--) work (); return 0;}
View Code
POJ 3304 segments to determine the intersection of lines and segments