Main topic: There is a room (upper left corner (0,10), lower right Corner (10,0)), and then the room has n wall, each wall has two doors, to find out from the initial point (0,5), to reach the end (10,5) the shortest distance.
Analysis: It is obvious that according to the shortest line between two points, so the route must be a point between the connection, only need to judge the two points between the wall can be known. The code is as follows:=========================================================================================================== ===========================
#include <math.h>#include<algorithm>#include<stdio.h>using namespacestd;Const intMAXN =1007;Const DoubleOO = 1e9+7;Const DoubleEPS = 1e-8;structpoint{Doublex, y, Len; Point (Doublex=0,Doubley=0,Doublelen=0): X (x), Y (y), Len (len) {} pointoperator- (ConstPoint &t)Const{ returnPoint (X-t.x, yt.y); } int operator*(ConstPoint &t)Const{ Doublec = x*t.y-y*T.x; if(C > EPS)return 1; if(Fabs (c) <eps)return 0; return-1; }};structwall{Point A, B; Wall (Point A=0, point b=0): A (a), B (b) {}};BOOLCanLine (Point A, point B, Wall w[],intN) { for(intI=0; i<n; i++) { if(W[i]. a.x < b.x | | W[i]. a.x >a.x)Continue; intt = (A-B) * (W[i]. A-B) + (A-B) * (W[i]. Bb); if(T = =0) return false; } return true;}intMain () {intM; while(SCANF ("%d", &m)! = EOF && M! =-1) { intI, J, nw=0, np=1; DoubleX, y[Ten]; Wall W[MAXN]; Point P[MAXN]; p[0] = Point (0,5,0); while(m--) {scanf ("%LF%LF%LF%LF%LF", &x, &y[0], &y[1], &y[2], &y[3]); P[NP+ +] = Point (x, y[0], oo), p[np++] = point (x, y[1], oo); P[NP+ +] = Point (x, y[2], oo), p[np++] = point (x, y[3], oo); W[NW+ +] = Wall (Point (X,-1), point (X, y[0])); W[NW+ +] = Wall (Point (X, y[1]), point (X, y[2])); W[NW+ +] = Wall (Point (X, y[3]), point (X, One)); } P[NP+ +] = Point (Ten,5, OO); for(i=1; i<np; i++) for(j=0; j<i; J + +) {Point T= P[i]-P[j]; T.len= sqrt (t.x*t.x+t.y*t.y); if(P[i].len > T.len + p[j].len && CanLine (P[i], P[j], W, nw) = =true) P[i].len= T.len +P[j].len; } printf ("%.2f\n", p[np-1].len); } return 0;}
The Doors-poj 1556 (segment intersection)