Ultraviolet A 5788 Wally World

Source: Internet
Author: User

Ultraviolet A 5788 Wally World

Https://icpcarchive.ecs.baylor.edu/index.php? Option = com_onlinejudge & Itemid = 8 & page = show_problem & problem = 3799

Two star-crossed lovers want to meet. The two lovers are standing at distinct points in the plane (but then again, aren't we all ?). They can travel freely fail t that there is a single wall which cannot be crossed. The wall is a line segment which is parallel to eitherXOrYAxis. Each lover can move 1 unit in 1 second. How long will it take them to be together if they both choose the best path?

Input

Input for each test case will consist of two lines each containing four integers. The first two integers will specifyXAndYCoordinates of the first lover; the next two integers will specifyXAndYCoordinates of the second lover. the next four integers will specify the start and end points of the wall. furthermore, in all cases both lovers will not be on the (infinite) line containing the wall -- that is, the wall extended in both directions ctions. all coordinates will be positive and less than or equal to 10000 and neither lover will start on the wall. the input will be terminated by a line containing four zeroes.

Output

For each test case, output the minimum time in seconds for the two lovers to meet. Print the answer to exactly 3 decimal places, using the output format shown in the example .,

Input

5 2 7 21 1 1 1001 2 3 22 1 2 1000 0 0 0
Output

Case 1: 1.000Case 2: 1.414

Question: A is the start point and B is the end point. It is required that the shortest path from A to B cannot be directly transmitted through the line segment CD.
Solution:
1. AB and CD do not overlap. ANS = AB
2. AB and CD intersect. ANS = MIN (AC + BC, AD + BD)
Code:
# Include
   
    
# Include
    
     
Struct point {double x; double y ;}; double direction (point p1, point p2, point p) {return (p1.x-p. x) * (p2.y-p. y)-(p2.x-p. x) * (p1.y-p. y);} int on_segment (point p1, point p2, point p) {double max = p1.x> p2.x? P1.x: p2.x; double min = p1.x <p2.x? P1.x: p2.x; if (p. x> = min & p. x <= max) return 1; elsereturn 0;} // determines whether the p1p2 line segment and p3p4 intersect int segments_intersert (point p1, point p2, point p3, point p4) {double d1, d2, d3, d4; d1 = direction (p1, p2, p3); d2 = direction (p1, p2, p4); d3 = direction (p3, p4, p1 ); d4 = direction (p3, p4, p2); if (d1 * d2 <0 & d3 * d4 <0) return 1; else if (d1 = 0 & on_segment (p1, p2, p3) return 1; else if (d2 = 0 & on_segment (p1, P2, p4) return 1; else if (d3 = 0 & on_segment (p3, p4, p1) return 1; else if (d4 = 0 & on_segment (p3, p4, p2) return 1; return 0 ;}int main () {point a, B, c, d; int C = 1; while (scanf ("% lf", &. x, &. y, & B. x, & B. y )! = EOF) {double ans; if (. x = 0 &. y = 0 & B. x = 0 & B. y = 0) return 0; scanf ("% lf", & c. x, & c. y, & d. x, & d. y); if (segments_intersert (a, B, c, d) {double ac = sqrt (. x-c. x) * (. x-c. x) + (. y-c. y) * (. y-c. y); double bc = sqrt (B. x-c. x) * (B. x-c. x) + (B. y-c. y) * (B. y-c. y); double ad = sqrt (. x-d. x) * (. x-d. x) + (. y-d. y) * (. y-d. y); double bd = sqrt (B. x-d. x) * (B. x-d. x) + (B. y-d. y) * (B. y-d. y); ans = ac + bc; if (ans> (ad + bd) ans = ad + bd;} elseans = sqrt (. x-B. x) * (. x-B. x) + (. y-B. y) * (. y-B. y); printf ("Case % d: %. 3lf \ n ", C ++, ans/2.0);} return 0 ;}
    
   

Related Article

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.