思路:
模拟。
实现:
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 ", 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 ", ans); 50 } 51 return 0; 52 }