POJ-1556 The Doors line segment intersection + shortest

Source: Internet
Author: User

In a rectangular plane, if there is a trunk wall, the shortest path from a point on the left to a point on the right is obtained.

Solution: the fact is that the line must pass through the points on both sides of the door, and it is impossible to pass through the middle. Therefore, we will enumerate whether the two points are reachable. Here we will use the determination of the intersection of the line segments. After creating the image, it is done by a spfa.

The Code is as follows:

# Include <iostream> # include <cstdio> # include <algorithm> # include <cmath> # include <cstring> using namespace STD; int N; struct wall {Double X,, b, c, d;} W [20]; struct point {Double X, Y;} e [105]; int idx; struct line {point A, B; line (point X, point y) {A = X, B = y;} friend bool cross (const line &, const line &);}; double G [105] [105]; bool cross (const line & line1, const line & line2) {double xA1 = Line1.a. x; double ya1 = line1.a. y; double xa2 = line1. B. x; double YA2 = line1. B. y; double xb1 = line2.a. x; double yb1 = line2.a. y; double XB2 = line2. B. x; double yb2 = line2. B. y; If (Xa2-Xa1) * (Yb1-Ya1)-(Xb1-Xa1) * (Ya2-Ya1) * (Xa2-Xa1) * (Yb2-Ya1) * (Xb2-Xa1)> 0) return false; If (Xb2-Xb1) * (Ya1-Yb1)-(Xa1-Xb1) * (Yb2-Yb1) * (Xb2-Xb1) * (Ya2-Yb1) * (Xa2-Xb1) * (Yb2-Yb1)> 0) return false; return true ;} Void insert (Double X, Double Y) {e [idx]. X = x, E [idx]. y = y; ++ idx;} bool legal (int x, int y) {line = line (E [X], E [y]); int L = (x-1)/4, r = (Y-1)/4; // calculate which wall these points belong, enumerate the middle wall if (x = 0) L =-1; // If x = 0, special processing is required for (INT I = L + 1; I <R; ++ I) {// determines whether the calculation is blocked by the wall. If (! Cross (line, line (E [I * 4 + 1], E [I * 4 + 2]) // if a door from the middle wall does not pass through &&! Cross (line, line (E [I * 4 + 3], E [I * 4 + 4]) {return false ;}} return true ;} double dist (const point & A, const point & B) {return SQRT (. x-B. x) * (. x-B. x) + (. y-B. y) * (. y-B. y);} void build () {for (INT I = 0; I <idx; ++ I) {for (Int J = I + 1; j <idx; ++ J) {// enumerate all pairs of combinations if (Legal (I, j) {G [I] [J] = dist (E [I], E [J]) ;}}}# include <queue> bool vis [105]; double dis [105]; void spfa () {MEMS Et (VIS, 0, sizeof (VIS); For (INT I = 0; I <idx; ++ I) {dis [I] = 10000000 ;} dis [0] = 0; queue <int> q; q. push (0); vis [0] = true; while (! Q. Empty () {int v = Q. Front (); q. Pop ();
Vis [v] = false; For (INT I = V + 1; I <idx; ++ I) {If (G [v] [I]! = 10000000 & dis [I]> dis [v] + G [v] [I]) {dis [I] = dis [v] + G [v] [I]; If (! Vis [I]) {vis [I] = true; q. push (I) ;}}}} int main () {While (scanf ("% d", & N), n! =-1) {for (INT I = 0; I <105; ++ I) {for (Int J = 0; j <105; ++ J) {G [I] [J] = 10000000 ;}} idx = 0; insert (0.0, 5.0); For (INT I = 0; I <n; ++ I) {scanf ("% lf", & W [I]. x, & W [I]. a, & W [I]. b, & W [I]. c, & W [I]. d); insert (W [I]. x, W [I]. a), insert (W [I]. x, W [I]. b); insert (W [I]. x, W [I]. c), insert (W [I]. x, W [I]. d); // read all the walls and add four dots} insert (10, 5); Build (); spfa (); printf ("%. 2f \ n ", DIS [idx-1]);} return 0 ;}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.