这题就是凸包入门题目,求凸包周长
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const int maxn=105;
struct point
{
double x;
double y;
double there;
} p[maxn],chs[maxn];
bool cmp1(point a,point b)
{
if(a.y==b.y)return a.x<b.x;
else
return a.y<b.y;
}
bool cmp2(point a,point b)
{
if(a.there==b.there)
return a.x<b.x;
else
return a.there<b.there;
}
double get_there(point a,point b)
{
return atan2((a.y-b.y),(a.x-b.x));
}
double dist_point2(point a,point b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
bool line_point1(point a,point b,point c)
{
double m;
m=(b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x);
if(m>0)return false;
if(m<0)return true;
if(m==0)return true;
return false;
}
int main()
{
int i;
int n;
while(scanf("%d",&n),n)
{
double len=0;
for(i=0; i<n ; i++)
{
scanf("%lf%lf",&p[i].x,&p[i].y);
}
if(n==1)
{
printf("0.00\n");
continue;//这里如果是break就WA了
}
if(n==2)
{
double sum;
sum=dist_point2(p[0],p[1]);
printf("%.2lf\n",sum);
continue;
}
sort(p, p+n, cmp1);
for(i=0; i<n; i++)
{
p[i].there=get_there(p[i],p[0]);
}
sort(p+1, p+n,cmp2);
chs[0]=p[0];
chs[1]=p[1];
chs[2]=p[2];
int top=2;
for(i=3; i<n; i++)
{
while(top>=1&&line_point1(chs[top-1],chs[top],p[i]))
{
top--;
}
chs[++top]=p[i];
}
top++;
for(i=0; i<top; i++)
{
len+=dist_point2(chs[i%top],chs[(i+1)%top]);
}
printf("%.2lf\n",len);
}
return 0;
}
HDU 1392 凸包入门题目,求周长
最新推荐文章于 2019-08-16 22:45:00 发布