UVA 5788 Wally World,5788wally

來源:互聯網
上載者:User

UVA 5788 Wally World,5788wally

地址: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 except 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 for each test case will consist of two lines each containing four integers. The first two integers will specify the x and y coordinates of the first lover; the next two integers will specify the x and ycoordinates 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. 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.

輸出

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.、

輸入

5 2 7 21 1 1 1001 2 3 22 1 2 1000 0 0 0
輸出

Case 1: 1.000Case 2: 1.414

題意: A為起點,B為終點。要求從A走到B不能直接通過線段CD的最短路程。
解題思路:
1、AB與CD不相交。ANS= AB
2、AB與CD相交。ANS=MIN(AC+BC,AD+BD)
代碼:
#include <stdio.h>#include <math.h>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;}//  判斷線段p1p2是否與p3p4相交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%lf%lf%lf", &a.x, &a.y, &b.x, &b.y) != EOF){double ans;if (a.x == 0 && a.y == 0 && b.x == 0 && b.y == 0)return 0;scanf("%lf%lf%lf%lf", &c.x, &c.y, &d.x, &d.y);if (segments_intersert(a, b, c, d)){double ac = sqrt((a.x - c.x)*(a.x - c.x) + (a.y - c.y)*(a.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((a.x - d.x)*(a.x - d.x) + (a.y - d.y)*(a.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((a.x - b.x)*(a.x - b.x) + (a.y - b.y)*(a.y - b.y));printf("Case %d: %.3lf\n", C++, ans / 2.0);}return 0;}




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.