比賽的時候曾經一度想到了這種做法。直接用數學關系推導,但是當時覺得可能不對,是以幹脆沒有寫。。。
沒想到了。。。
轉載一篇代碼:
#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;
}