hdu 1392:Surround the Trees(计算几何,求凸包周长)

本文介绍了一道计算几何题目——求凸包周长。题目要求使用绳子包围平面上的所有点,并求出绳子的最短长度。文章提供了完整的解决思路及C++代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.



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
 

 

Source
 

 

Recommend
Ignatius.L   |   We have carefully selected several similar problems for you:   2108  2150  1558  1374  1391 
 
  计算几何:求凸包周长
  题意
  给你n棵树的坐标,你要用一根绳子包围所有的树,忽略树的半径和高度,求这根绳子的最短长度。
  思路
  这是求计算几何中的凸包周长。
  注意
  n=1或n=2的情况需要特殊考虑。
  代码
 1 #include <iostream>
 2 #include <iomanip>
 3 #include <cmath>
 4 using namespace std;
 5 
 6 struct Point{
 7     double x,y;
 8 }p[105],pl[105];
 9 double dis(Point p1,Point p2)
10 {
11     return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
12 }
13 double xmulti(Point p1,Point p2,Point p0)    //求p1p0和p2p0的叉积,如果大于0,则p1在p2的顺时针方向
14 {
15     return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
16 }
17 double graham(Point p[],int n)    //点集和点的个数 
18 {
19     int pl[105];
20     //找到纵坐标(y)最小的那个点,作第一个点 
21     int i;
22     int t = 1;
23     for(i=1;i<=n;i++)
24         if(p[i].y < p[t].y)
25             t = i;
26     pl[1] = t;
27     //顺时针找到凸包点的顺序,记录在 int pl[]
28     int num = 1;    //凸包点的数量
29     do{    //已确定凸包上num个点 
30         num++; //该确定第 num+1 个点了
31         t = pl[num-1]+1;
32         if(t>n) t = 1;
33         for(int i=1;i<=n;i++){    //核心代码。根据叉积确定凸包下一个点。 
34             double x = xmulti(p[i],p[t],p[pl[num-1]]);
35             if(x<0) t = i;
36         }
37         pl[num] = t;
38     } while(pl[num]!=pl[1]);
39     //计算凸包周长 
40     double sum = 0;
41     for(i=1;i<num;i++)
42         sum += dis(p[pl[i]],p[pl[i+1]]);
43     return sum;
44 }
45 int main()
46 {
47     int n;
48     while(cin>>n){
49         if(n==0) break;
50         int i;
51         for(i=1;i<=n;i++)
52             cin>>p[i].x>>p[i].y;
53         if(n==1){
54             cout<<0<<endl;
55             continue;
56         }
57         if(n==2){
58             cout<<setiosflags(ios::fixed)<<setprecision(2);
59             cout<<dis(p[1],p[2])<<endl;
60             continue;
61         }
62         cout<<setiosflags(ios::fixed)<<setprecision(2);
63         cout<<graham(p,n)<<endl;
64     }
65     return 0;
66 }

 

Freecode : www.cnblogs.com/yym2013

转载于:https://www.cnblogs.com/yym2013/p/3634513.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值