UESTCoj 1874 Wally World (Computational ry)

Source: Internet
Author: User
Tags x2 y2

Http://acm.uestc.edu.cn/problem.php? Pid = 1874 & cid = 217

Wally WorldTime Limit: 1000 MS Memory Limit: 65535 kB Solved: 26 Tried: 135 Description

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 should t that there is a single wall which cannot be crossed. The wall is a line segment which is parallel to either the x or y axis.
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 rst two integers will specify the x and y coordinates of the rst lover; the next two integers will specify the x and y coordinates 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 (in nite) 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.

Sample Input

5 2 7 2
1 1 100
1 2 3 2
2 1 2 100
0 0 0 0

Sample Output

Case 1: 1.000
Case 2: 1.414

Ideas:

Find the shortest distance between two points. There is a wall in the middle of the two points. You need to determine whether the two points can be reached directly. If not, you need to bypass the shortest distance of the wall and divide it by 2.

Input:

X1 y1 x2 y2 // coordinates of two lovers

X3 y3 x4 y4 // coordinate of the two ends of the wall

Analysis:

Obtain the straight line y = func (x) represented by (x1, y1), (x2, y2). If x3 = x4, replace x3 with y = func (x) then, judge whether y is within the range [y1, y2], [y3, y4], and x3 is within the range [x1, x2, only when these three intervals are met can the online segments be considered to be intersecting. Otherwise, the online segments are not intersecting;

In addition, we need to consider whether the slope of a straight line exists. When the slope is not there, we need to make a special decision. This step is very important.

The following is the AC code:

1 # include <stdio. h> 2 # include <stdlib. h> 3 # include <iostream> 4 # include <math. h> 5 using namespace std; 6 # define INF 9999999999 7 8 // judge whether x is between a and B 9 int isOK (double x, int a, int B) 10 {11 int temp; 12 if (a> B) 13 {14 temp = a, a = B, B = temp; 15} 16 if (x <= B & x> = a) 17 {18 return 1; 19} 20 return 0; 21} 22 23 // obtain the distance between two points: 24 double distance (int x1, int y1, int x2, int y2) 25 {26 return sqrt (double) (x1- X2) * (x1-x2) + (y1-y2) * (y1-y2); 27} 28 29 // returns a smaller number of 30 double mind (double a, double B) 31 {32 return a <B? A: B; 33} 34 35 // processing function 36 double deal (double x, int a, int B, double y, int a1, int b1, double xx, int a2, int b2, int x1, int x2, int x3, int x4, int y1, int y2, int y3, int y4) 37 {38 double res; 39 if (isOK (x, a, B) & isOK (y, a1, b1) & isOK (xx, a2, b2) 40 {41 double d1 = distance (x1, y1, x3, y3) + distance (x2, y2, x3, y3); 42 double d2 = distance (x1, y1, x4, y4) + distance (x2, y2, x4, y4 ); 43 res = mind (d1, d2); 44} 45 else 46 {47 res = di Stance (x1, y1, x2, y2); 48} 49 return res; 50} 51 52 int main () 53 {54 int x1, x2, x3, x4, y1, y2, y3, y4; 55 int cnt = 1; 56 while (~ Scanf ("% d", & x1, & y1, & x2, & y2) & (x1 + x2 + y1 + y2 )) 57 {58 scanf ("% d", & x3, & y3, & x4, & y4); 59 double k, B; 60 if (x1 = x2) // when the slope does not exist, 61 {62 k = INF; 63 B = 0; 64} 65 else 66 {67 k = (double) (y1-y2)/(x1-x2); 68 B = y1-k * x1; 69} 70 double y, x; 71 double res; 72 if (x3 = x4) 73 {74 if (x1 = x2) // when the slope does not exist and the slope of another line segment does not exist, the two lines are parallel, the result is 75 {76 res = distance (x1, y1, x2, y2); 77 printf ("Case % d: %. 3lf \ n ", cnt ++, res/2.0); 78 continue; 79} 80 y = k * x3 + B; 81 res = deal (y, y1, y2, x3, x1, x2, y, y3, y4, x1, x2, x3, x4, y1, y2, y3, y4); 82} 83 else if (y3 = y4) 84 {85 if (x1 = x2) // The slope does not exist, but the two straight lines are vertical 86 {87 x = x1; 88} 89 else if (fabs (k) <1e-6) 90 {91 x = B; 92} 93 else 94 {95 x = (double) (y3-b)/k; 96} 97 res = deal (x, x1, x2, y3, y1, y2, x, x3, x4, x1, x2, x3, x4, y1, y2, y3, y4); 98} 99 printf ("Case % d: %. 3lf \ n ", cnt ++, res/2.0); 100} 101 102 return 0; 103}

 

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.