CF1079D Barcelonian Distance

思路:

模拟。

实现:

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const long long INF = 10000000000;
 4 double dis(double x1, double y1, double x2, double y2)
 5 {
 6     return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
 7 }
 8 int main()
 9 {
10     int a, b, c;
11     double x1, x2, y1, y2;
12     while (cin >> a >> b >> c)
13     {
14         cin >> x1 >> y1 >> x2 >> y2;
15         if (a == 0 || b == 0)
16         {
17             printf("%.10f\n", fabs(x1 - x2) + fabs(y1 - y2));
18             continue;
19         }
20         double n1 = x1, m1 = y2; // y1--y2
21         double ty = (-a * x1 - c) / b;
22         double tx = (-b * y2 - c) / a;
23         double ans = fabs(x1 - x2) + fabs(y1 - y2);
24         if (ty >= min(y1, y2) && ty <= max(y1, y2) && tx >= min(x1, x2) && tx <= max(x1, x2))
25         {
26             double tmp = fabs(y1 - ty) + fabs(x2 - tx) + dis(x1, ty, tx, y2);
27             ans = min(ans, tmp);
28         }
29         double ty2 = (-a * x2 - c) / b;
30         if (ty >= min(y1, y2) && ty <= max(y1, y2) && ty2 >= min(y1, y2) && ty2 <= max(y1, y2))
31         {
32             double tmp = fabs(y1 - ty) + fabs(ty2 - y2) + dis(x1, ty, x2, ty2);
33             ans = min(ans, tmp);
34         }
35         double n2 = x2, m2 = y1; // x1--x2
36         tx = (-b * y1 - c) / a;
37         ty = (-a * x2 - c) / b;
38         if (ty >= min(y1, y2) && ty <= max(y1, y2) && tx >= min(x1, x2) && tx <= max(x1, x2))
39         {
40             double tmp = fabs(x1 - tx) + fabs(y2 - ty) + dis(tx, y1, x2, ty);
41             ans = min(ans, tmp);
42         }
43         double tx2 = (-b * y2 - c) / a;
44         if (tx >= min(x1, x2) && tx <= max(x1, x2) && tx2 >= min(x1, x2) && tx2 <= max(x1, x2))
45         {
46             double tmp = fabs(x1 - tx) + fabs(tx2 - x2) + dis(tx, y1, tx2, y2);
47             ans = min(ans, tmp);
48         }
49         printf("%.10f\n", ans);
50     }
51     return 0;
52 }

 

转载于:https://www.cnblogs.com/wangyiming/p/10054315.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值