HDU1392(凸包)

Posted Penn000

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU1392(凸包)相关的知识,希望对你有一定的参考价值。

Surround the Trees

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 10299    Accepted Submission(s): 3991


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

 
Graham扫描法求凸包,凸包周长即为答案。
 1 //2016.10.2
 2 #include <iostream>
 3 #include <cstdio>
 4 #include <cstring>
 5 #include <cmath>
 6 #include <algorithm>
 7 #define N 105
 8 #define eps 1e-8
 9 
10 using namespace std;
11 
12 struct point
13 {
14     double x, y;
15     point(){}
16     point(double a, double b):x(a), y(b){}
17     point operator-(point a){//向量减法
18         return point(x-a.x, y-a.y);
19     }
20     point operator+(point a){//向量加法
21         return point(x+a.x, y+a.y);
22     }
23     double operator*(point a){//向量叉积
24         return x*a.y-y*a.x;
25     }
26     bool operator<(const point a)const{
27         if(fabs(x-a.x)<eps)return y<a.y;//浮点数的判等不能直接用‘==’直接比较
28         return x<a.x;
29     }
30     double len(){//向量的模
31         return sqrt(x*x+y*y);
32     }
33 }p[N], s[N];//p为点,s为栈
34 
35 double cp(point a, point b, point o)//向量oa,ob叉积
36 {
37     return (a-o)*(b-o);
38 }
39 
40 void Convex(point *p, int &n)//Graham扫描法,栈内为所有凸包点
41 {
42     sort(p, p+n);
43     int top, m;
44     s[0] = p[0]; s[1] = p[1]; top = 1;
45     for(int i = 2; i < n; i++)//从前往后扫
46     {
47         while(top>0 && cp(p[i], s[top], s[top-1])>=0)top--;
48         s[++top] = p[i];
49     }
50     m = top;
51     s[++top] = p[n-2];
52     for(int i = n-3; i >= 0; i--)//从后往前扫
53     {
54         while(top>m && cp(p[i], s[top], s[top-1])>=0)top--;
55         s[++top] = p[i];
56     }
57     n = top;
58 }
59 
60 int main()
61 {
62     int n;
63     while(scanf("%d", &n)!=EOF && n)
64     {
65         for(int i = 0; i < n; i++)
66               scanf("%lf%lf", &p[i].x, &p[i].y);
67         sort(p, p+n);
68         int cnt = 0;
69         for(int i = 1; i < n; i++)//去掉重复的点
70               if(fabs(p[i].x-p[cnt].x)>eps || fabs(p[i].y-p[cnt].y)>eps)
71                   p[++cnt] = p[i];
72         cnt++;
73         if(cnt == 1){
74             printf("0.00\n");continue;
75         }else if(cnt==2){
76             printf("%.2lf\n", (p[1]-p[0]).len());continue;
77         }
78         Convex(p, cnt);
79         double ans = 0;
80         s[cnt] = s[0];
81         for(int i = 0; i < cnt; i++)ans+=(s[i+1]-s[i]).len();
82         printf("%.2lf\n", ans);
83     }
84 
85     return 0;
86 }

 

 

以上是关于HDU1392(凸包)的主要内容,如果未能解决你的问题,请参考以下文章

HDU 1392 Surround the Trees (凸包周长)

HDU1392(凸包)

计算几何(凸包模板):HDU 1392

HDU 1392 凸包

hdu1392凸包裸题

HDU ACM 1392 Surround the Trees-&gt;凸包