原题传送:http://acm.hdu.edu.cn/showproblem.php?pid=3694
题意很简单,求平面四个点的费马点。
这道题直接枚举所有情况:
1.四个点独立不重叠,这种情况又分为两种情况:
a.四个点构成凸四边形,那么对角线交点就是费马点
b.四个点构成凹四边形,那么费马点肯定是凹的那个点 (因为漏了这个情况wa了10多次)
2.四个点中有重复的点,费马点就是重复的那个点
View Code
1 #include <stdio.h> 2 #include <string.h> 3 #include <math.h> 4 #include <stdlib.h> 5 #define INF 10000000.0 6 const double eps = 1e-6; 7 8 struct point{double x, y;}; 9 10 inline double min(double a, double b){return a < b ? a : b;} 11 12 inline double max(double a, double b){return a > b ? a : b;} 13 14 bool inter(point a, point b, point c, point d) // 判相交 15 { 16 if(min(a.x, b.x) > max(c.x, d.x) || 17 min(a.y, b.y) > max(c.y, d.y) || 18 min(c.x, d.x) > max(a.x, b.x) || 19 min(c.y, d.y) > max(a.y, b.y) ) 20 return 0; 21 22 double h, i, j, k; 23 h = (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x); 24 i = (b.x - a.x) * (d.y - a.y) - (b.y - a.y) * (d.x - a.x); 25 j = (d.x - c.x) * (a.y - c.y) - (d.y - c.y) * (a.x - c.x); 26 k = (d.x - c.x) * (b.y - c.y) - (d.y - c.y) * (b.x - c.x); 27 return h * i <= eps && j * k <= eps; 28 } 29 30 double cal(point a, point b) // 计算两点距离 31 { 32 return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y)); 33 } 34 35 int main() 36 { 37 point a, b, c, d; 38 double len[20]; 39 while(scanf("%lf%lf%lf%lf%lf%lf%lf%lf", &a.x, &a.y, &b.x, &b.y, &c.x, &c.y, &d.x, &d.y)) 40 { 41 if(a.x == -1) 42 break; 43 for(int i = 1; i < 20; i ++) 44 len[i] = INF; 45 46 if(a.x == d.x && a.y == d.y) 47 len[1] = cal(a, b) + cal(a, c); //a,d重合 48 else if(a.x == c.x && a.y == c.y) 49 len[2] = cal(a, b) + cal(a, d); //a,c重合 50 else if(a.x == b.x && a.y == b.y) 51 len[3] = cal(a, c) + cal(a, d); //a,b重合 52 else if(b.x == c.x && b.y == c.y) 53 len[4] = cal(b, a) + cal(b, d); //b,c重合 54 else if(b.x == d.x && b.y == d.y) 55 len[5] = cal(b, a) + cal(b, c); //b,d重合 56 else if(c.x == b.x && c.y == b.y) 57 len[6] = cal(c, a) + cal(c, b); //c,d重合 58 else 59 { 60 if(inter(a, b, c, d)) //ab, cd相交 61 len[7] = cal(a, b) + cal(c, d); 62 else if(inter(a, c, b, d)) //ac, bd相交 63 len[8] = cal(a, c) + cal(b, d); 64 else if(inter(a, d, b, c)) //ad, bc相交 65 len[9] = cal(a, d) + cal(b, c); 66 } 67 len[10] = cal(a, b) + cal(a, c) + cal(a, d); // 处理凹多边形 68 len[11] = cal(b, a) + cal(b, c) + cal(b, d); 69 len[12] = cal(c, a) + cal(c, b) + cal(c, d); 70 len[13] = cal(d, a) + cal(d, b) + cal(d, c); 71 double m = INF; 72 for(int i = 1; i < 20; i ++) 73 m = min(len[i], m); 74 printf("%.4lf\n", m); 75 } 76 return 0; 77 }