链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=453
Time Limit: 2 Seconds Memory Limit: 65536 KB
There are a lot of trees in an area. A peasant wants to buy a rope to surround all these trees. So at first he must know the minimal required length of the rope. However, he does not know how to calculate it. Can you help him?
The diameter and length of the trees are omitted, which means a tree can be seen as a point. The thickness of the rope is also omitted which means a rope can be seen as a line.
There are no more than 100 trees.
Input
The input contains one or more data sets. At first line of each input data set is number of trees in this data set, it is followed by series of coordinates of the trees. Each coordinate is a positive integer pair, and each integer is less than 32767. Each pair is separated by blank.
Zero at line for number of trees terminates the input for your program.
Output
The minimal length of the rope. The precision should be 10^-2.
Sample Input
9
12 7
24 9
30 5
41 9
80 7
50 87
22 9
45 1
50 7
0
Sample Output
243.06
-------------------------------------------------------------
题意很明了,就是凸包求取周长,看了别人模板,看了两天还是似懂非懂,最后照模板敲了,真不知道什么时候才可以真正掌握
AC不易,且行且珍惜……
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#include <math.h>
using namespace std; #define MAX 110
const double eps=1e-; typedef struct
{
double x,y;
}point; point c[MAX]; bool dy(double x,double y)
{
return x>y+eps;
}
bool xy(double x,double y)
{
return x<y-eps;
}
bool xyd(double x,double y)
{
return x<y+eps;
}
bool dyd(double x,double y)
{
return x>y-eps;
}
bool dd(double x,double y)
{
return fabs(x-y)<eps;
} double crossProduct(point a,point b,point c)
{
return (c.x-a.x)*(b.y-a.y)-(c.y-a.y)*(b.x-a.x);
}
double dist(point a,point b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
} bool cmp(point a,point b)
{
if(dd(a.y,b.y))
return xy(a.x,b.x);
return xy(a.y,b.y);
}
bool cmp1(point a,point b)
{
double len=crossProduct(c[],a,b);
if(dd(len,0.0))
return xy(dist(c[],a),dist(c[],b));
return xy(len,0.0);
} point stk[MAX];
int top; double dis()
{
double sum=0.0;
for(int i=;i<top;i++)
{
sum+=dist(stk[i],stk[i+]);
}
sum+=dist(stk[top],stk[]);
return sum;
} double Graham(int n)
{
sort(c,c+n,cmp);
sort(c+,c+n,cmp1);
top=;
stk[top++]=c[];
stk[top++]=c[];
stk[top++]=c[];
top--;
for(int i=;i<n;i++)
{
while()
{
point a,b;
a=stk[top];
b=stk[top-];
if(xyd(crossProduct(a,b,c[i]),0.0))
{
top--;
}
else
break;
}
stk[++top]=c[i];
}
return dis();
} int main()
{
int i,j,k,t;
int n,m;
while(scanf("%d",&n)!=EOF&&n)
{
for(i=;i<n;i++)
{
scanf("%lf%lf",&c[i].x,&c[i].y);
}
if(n==)
{
printf("0.00\n");
//continue;
}
else if(n==)
{
printf("%.2lf\n",*dist(c[],c[]));
//continue;
}
else
{
printf("%.2lf\n",Graham(n));
}
}
return ;
}