Surround the Trees
题目链接:http://acm.tzc.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=1255
Surround the Trees
Total Submit: 175 Accepted: 67
Description
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
分析:
简单的凸包问题。
代码:
View Code #include<iostream>
#include<cmath>
using namespace std;
struct Point
{
double x, y;
};
const int MAXN=105;
Point p[MAXN]; //存放所有的点
Point result[MAXN]; //存放凸包上的点
int top;
double xmult(Point a, Point b, Point c)
{
return (b.x-a.x)*(c.y-a.y)-(c.x-a.x)*(b.y-a.y);
}
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 cmp(const void *a, const void *b) //以左下角的点为基点,其它点与该点的连线与x轴所成的角度,从小到大排序
{
Point p1=*(Point*)a;
Point p2=*(Point*)b;
double x=xmult(p[0], p1, p2);
if (x<0) return 1;
if (x==0 && dist(p[0], p1)<dist(p[0], p2)) return 1;
return -1;
}
void create_convex(int n) //生成凸包
{
result[0]=p[0];
result[1]=p[1];
result[2]=p[2];
int i;
top=2;
for (i=3; i<=n; i++)
{
while (xmult(result[top-1], result[top], p[i])<=0 && top>=1) top--;
result[++top]=p[i];
}
}
int main()
{
int n;
while (cin>>n, n)
{
int i;
for (i=0; i<n; i++)
{
cin>>p[i].x>>p[i].y;
}
if (n==1)
{
puts("0.00");
continue;
}
if (n==2)
{
double len=dist(p[0], p[1]);
printf("%.2lf\n", len);
continue;
}
Point tmp;
int pos=0;
tmp.x=p[0].x; tmp.y=p[0].y;
for (i=1; i<n; i++) //寻找左下角的点
{
if (p[i].y<tmp.y)
{
tmp.x=p[i].x;
tmp.y=p[i].y;
pos=i;
}
if (p[i].y==tmp.y && p[i].x<tmp.x)
{
tmp.x=p[i].x;
tmp.y=p[i].y;
pos=i;
}
}
swap(p[0], p[pos]);
/* tmp=p[0];
p[0]=p[pos];
p[pos]=tmp;*/
qsort(&p[1], n-1, sizeof(p[0]), cmp);
p[n]=p[0];
create_convex(n);
double len=0.0;
for (i=1; i<=top; i++)
{
len+=dist(result[i-1], result[i]);
}
printf("%.2lf\n", len);
}
return 0;
}

浙公网安备 33010602011771号