比赛的时候曾经一度想到了这种做法。直接用数学关系推导,但是当时觉得可能不对,所以干脆没有写。。。
没想到了。。。
转载一篇代码:
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
struct Point{
double x;
double y;
}p[5];
double dist(Point a, Point b)
{
return sqrt((a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y));
}
int main()
{
while(scanf("%lf%lf", &p[0].x, &p[0].y) != EOF)
{
for(int i = 1; i < 4; i++)
{
scanf("%lf%lf", &p[i].x, &p[i].y);
}
double d1 = dist(p[0], p[1]);
double d2 = dist(p[1], p[2]);
double d3 = dist(p[2], p[3]);
double d4 = dist(p[0], p[3]);
double d5 = dist(p[0], p[2]);
double d6 = dist(p[1], p[3]);
double ans = min(d1+d3, min(d4+d2, d5+d6));
printf("%.8lf\n", ans);
}
return 0;
}