题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1392
凸包模板题,但是特别注意只有两个点的情况,MD两个点只能输出两点间的距离,可是题目上明明说的是surround围绕啊,MDZZ。
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
const double eps = 1e-10;
struct Point
{
double x, y;
Point() {}
Point(double x_, double y_): x(x_), y(y_) {}
bool operator < (const Point& a) const
{
if (x == a.x)
return y < a.y;
return x < a.x;
}
bool operator == (const Point& a) const
{
if (x == a.x && y == a.y)
return true;
return false;
}
};
typedef Point Vector;
Point P[110], convex[110];
Vector operator - (Point A, Point B)
{
return Vector(A.x-B.x, A.y-B.y);
}
double Length(Point A, Point B)
{
return sqrt((A.x-B.x)*(A.x-B.x) + (A.y-B.y)*(A.y-B.y));
}
double Cross(Vector A, Vector B) //A × B
{
return A.x*B.y - A.y*B.x;
}
int ConvexHull(Point *p, int n, Point *ch)
{
int m = 0;
sort(p, p+n);
n = unique(p, p+n) - p;
for (int i=0; i<n; i++)
{
while (m > 1 && Cross(ch[m-1]-ch[m-2], p[i]-ch[m-2]) <= 0) m--;
ch[m++] = p[i];
}
int k = m;
for (int i=n-2; i>=0; i--)
{
while (m > k && Cross(ch[m-1]-ch[m-2], p[i]-ch[m-2]) <= 0) m--;
ch[m++] = p[i];
}
return m;
}
int main()
{
int n;
while (scanf("%d",&n))
{
if (!n)
break;
for (int i=0; i<n; i++)
scanf("%lf%lf",&P[i].x, &P[i].y);
if (n == 1)
{
printf("%.2lf\n",0);
continue;
}
else if (n == 2)
{
printf("%.2lf\n",Length(P[0],P[1]));
continue;
}
int cnt = ConvexHull(P, n, convex);
double ans = 0;
for (int i=1; i<cnt; i++)
ans += Length(convex[i-1],convex[i]);
printf("%.2lf\n",ans);
}
return 0;
}