HDU 1392 Surround the Trees
Surround the Trees
Time
Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K
(Java/Others)
Total Submission(s): 3312 Accepted Submission(s):
1268
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.
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.
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
简单的凸包模板题目:
1 #include<stdio.h>
2 #include<stdlib.h>
3 #include<math.h>
4 typedef struct
5 {
6 double x , y ;
7 }POINT ;
8 POINT result[110] ;// 模拟堆栈S,保存凸包上的点
9 POINT tree[110] ;
10 int n , top ;
11 double Distance ( POINT p1 , POINT p2 )
12 {
13 return sqrt( (p1.x - p2.x)*(p1.x - p2.x) + (p1.y - p2.y)*(p1.y - p2.y) ) ;
14 }
15 double Multiply(POINT p1 , POINT p2 , POINT p3) // 叉积
16 {
17 return ( (p2.x - p1.x)*(p3.y - p1.y) - (p2.y - p1.y)*(p3.x - p1.x) ) ;
18 }
19 int cmp ( const void *p1 , const void *p2 )
20 {
21 POINT *p3,*p4;
22 double m;
23 p3 = (POINT *)p1;
24 p4 = (POINT *)p2;
25 m = Multiply(tree[0] , *p3 , *p4) ;
26 if(m < 0) return 1;
27 else if(m == 0 && (Distance(tree[0] , *p3) < Distance(tree[0],*p4)))
28 return 1;
29 else return -1;
30 }
31 void Tubao ()
32 {
33 int i ;
34 result[0].x = tree[0].x;
35 result[0].y = tree[0].y;
36 result[1].x = tree[1].x;
37 result[1].y = tree[1].y;
38 result[2].x = tree[2].x;
39 result[2].y = tree[2].y;
40 top = 2;
41 for ( i = 3 ; i <= n ; ++ i )
42 {
43 while (Multiply(result[top - 1] , result[top] , tree[i]) <= 0 )
44 top -- ; //出栈
45 result[top + 1].x = tree[i].x ;
46 result[top + 1].y = tree[i].y ;
47 top ++ ;
48 }
49
50 }
51 int main ()
52 {
53 int pos ;
54 double len , temp , px , py ;
55 while ( scanf ( "%d" , &n ) != EOF , n )
56 {
57 py = -1 ;
58 for ( int i = 0 ; i < n ; ++ i )
59 {
60 scanf ( "%lf%lf" , &tree[i].x , &tree[i].y ) ;
61
62 }
63 if ( n == 1 )
64 {
65 printf ( "0.00\n" ) ;
66 continue ;
67 }
68 else if ( n == 2 )
69 {
70 printf ( "%.2lf\n" , Distance(tree[0] , tree[1]) ) ;
71 continue ;
72 }
73 for ( int i = 0 ; i < n ; ++ i )
74 {
75 if(py == -1 || tree[i].y < py)
76 {
77 px = tree[i].x;
78 py = tree[i].y;
79 pos = i;
80 }
81 else if(tree[i].y == py && tree[i].x < px)
82 {
83 px = tree[i].x;
84 py = tree[i].y;
85 pos = i;
86 }
87 }
88 temp = tree[0].x ; // 找出y最小的点
89 tree[0].x = tree[pos].x ;
90 tree[pos].x = temp ;
91 temp = tree[0].y ;
92 tree[0].y = tree[pos].y ;
93 tree[pos].y = temp ;
94 qsort(&tree[1],n - 1,sizeof(double) * 2,cmp);
95 tree[n].x = tree[0].x;
96 tree[n].y = tree[0].y;
97 Tubao();
98 len = 0.0;
99 for(int i = 0 ; i < top ; i ++)
100 len = len + Distance(result[i] , result[i+1]) ;
101 printf("%.2lf\n",len);
102
103 }
104 return 0 ;
105 }