POJ 1556-the Doors
Test instructions
In the 10x10 space there are a lot of vertical walls, can not wear walls, ask you from (0,5) to (10,5) the shortest distance is how much.
Analysis:
Either direct or it must be the connection between the edge points of the wall and the starting and ending points.
So first enumerate the lines of each point on the wall to the other point to reach the distance, that is, to determine whether the segment intersects the wall (without the endpoint).
Then the shortest circuit.
1#include <iostream>2#include <cstdio>3#include <cmath>4#include <cstring>5 using namespacestd;6 Const DoubleINF =1e10;7 Const DoubleEPS = 1e-Ten;8 intDCMP (Doublex)9 {Ten returnFabs (x) < EPS?0: (X <0? -1:1); One } A struct Point - { - Doublex, y; thePoint (DoubleX1 =0,DoubleY1 =0): X (x1), Y (y1) {} - }; -Pointoperator-(point A,point B) - { + returnPoint (a.x-b.x, A.Y-b.y); - } + DoubleDet (point a,point B) A { at returna.x * B.Y-A.Y *b.x; - } - DoubleDot (point a,point B) - { - returna.x * b.x + a.y *b.y; - } in DoubleLength (Point a) - { to returnsqrt (Dot (A, a)); + } - BOOLonsegment (Point P, point A1, point A2) the { * returnDCMP (Det (A1-p, a2-p)) = =0&& dcmp (Dot (A1-p, a2-p)) <=0; $ }Panax Notoginseng struct Line - { the Point s,e; + Line () {} A Line (point S1, Point E1): S (S1), E (E1) {} the }; + BOOLSegcross (Point A1, Point A2, point B1, point B2) - { $ DoubleC1 = Det (A2-A1, B1-A1); $ DoubleC2 = Det (a2-a1, B2-A1); - DoubleC3 = Det (B2-B1, A1-B1); - DoubleC4 = Det (b2-b1, A2-B1); the if(DCMP (C1) * DCMP (C2) <0&& dcmp (C3) * DCMP (C4) <0)return 1; - Else return 0;Wuyi } the - intN; WuLine l[ -]; -Point p[ -]; About Doublemap[ -][ -]; $ intMain () - { - while(~SCANF ("%d", &n) && n! =-1) - { Ap[0] = Point (0,5); +p[1] = Point (Ten,5); the intt =2, M =0; - for(inti =1; I <= N; i++) $ { the Doublex; Point P1,p2,p3,p4; thescanf"%LF%LF%LF%LF%LF", &x, &p1.y, &p2.y, &P3.Y, &p4.y); thep1.x = p2.x = p3.x = p4.x =x; thel[m++] = line (point (X,0), p1); -l[m++] =Line (P2, p3); inl[m++] = line (P4, point (X,Ten)); thep[t++] = p1; p[t++] = p2; p[t++] = p3; p[t++] =P4; the } About for(inti =0; I < T; i++) the for(intj =0; J < T; J + +) theMAP[I][J] =INF; the for(inti =0; I < T; i++) + { - for(intj = i+1; J < T; J + +) the {Bayi if(p[i].x = = p[j].x)Continue; the BOOLFlag =1; the for(intK =0; K < M; k++)//Enumerate Walls - { - if(Segcross (P[i], p[j], L[K].E, L[K].S)) the { theFlag =0; Break; the } the } - if(flag) the { theMAP[I][J] = Map[j][i] = Length (p[i]-p[j]); the }94 the } the } the for(inti =0; I < T; i++) Map[i][i] =0;98 for(intK =0; K < T; k++) About for(inti =0; I < t;i++) - for(intj =0; J < T; J + +)101Map[i][j] = min (Map[i][j], map[i][k] +map[k][j]);102printf"%.2f\n", map[0][1]);103 }104}
View Code
POJ 1556-the Doors segments intersect with no endpoints