Surround the Trees
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6812 Accepted Submission(s): 2594
Problem 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.
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
12 7
24 9
30 5
41 9
80 7
50 87
22 9
45 1
50 7
0
Sample Output
243.06
Source
Recommend
计算几何:求凸包周长。
题意:
给你n棵树的坐标,你要用一根绳子包围所有的树,忽略树的半径和高度,求这根绳子的最短长度。
给你n棵树的坐标,你要用一根绳子包围所有的树,忽略树的半径和高度,求这根绳子的最短长度。
思路:
这是求计算几何中的凸包周长。
注意:
n=1或n=2的情况需要特殊考虑。
代码:
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std; struct Point{
double x,y;
}p[],pl[];
double dis(Point p1,Point p2)
{
return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
}
double xmulti(Point p1,Point p2,Point p0) //求p1p0和p2p0的叉积,如果大于0,则p1在p2的顺时针方向
{
return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
}
double graham(Point p[],int n) //点集和点的个数
{
int pl[];
//找到纵坐标(y)最小的那个点,作第一个点
int i;
int t = ;
for(i=;i<=n;i++)
if(p[i].y < p[t].y)
t = i;
pl[] = t;
//顺时针找到凸包点的顺序,记录在 int pl[]
int num = ; //凸包点的数量
do{ //已确定凸包上num个点
num++; //该确定第 num+1 个点了
t = pl[num-]+;
if(t>n) t = ;
for(int i=;i<=n;i++){ //核心代码。根据叉积确定凸包下一个点。
double x = xmulti(p[i],p[t],p[pl[num-]]);
if(x<) t = i;
}
pl[num] = t;
} while(pl[num]!=pl[]);
//计算凸包周长
double sum = ;
for(i=;i<num;i++)
sum += dis(p[pl[i]],p[pl[i+]]);
return sum;
}
int main()
{
int n;
while(cin>>n){
if(n==) break;
int i;
for(i=;i<=n;i++)
cin>>p[i].x>>p[i].y;
if(n==){
cout<<<<endl;
continue;
}
if(n==){
cout<<setiosflags(ios::fixed)<<setprecision();
cout<<dis(p[],p[])<<endl;
continue;
}
cout<<setiosflags(ios::fixed)<<setprecision();
cout<<graham(p,n)<<endl;
}
return ;
}
Freecode : www.cnblogs.com/yym2013