标签:style blog http color os strong io width
链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=453
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
-------------------------------------------------------------
题意很明了,就是凸包求取周长,看了别人模板,看了两天还是似懂非懂,最后照模板敲了,真不知道什么时候才可以真正掌握
AC不易,且行且珍惜……
1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4 #include <iostream> 5 #include <algorithm> 6 #include <math.h> 7 using namespace std; 8 9 #define MAX 110 10 const double eps=1e-6; 11 12 typedef struct 13 { 14 double x,y; 15 }point; 16 17 point c[MAX]; 18 19 bool dy(double x,double y) 20 { 21 return x>y+eps; 22 } 23 bool xy(double x,double y) 24 { 25 return x<y-eps; 26 } 27 bool xyd(double x,double y) 28 { 29 return x<y+eps; 30 } 31 bool dyd(double x,double y) 32 { 33 return x>y-eps; 34 } 35 bool dd(double x,double y) 36 { 37 return fabs(x-y)<eps; 38 } 39 40 double crossProduct(point a,point b,point c) 41 { 42 return (c.x-a.x)*(b.y-a.y)-(c.y-a.y)*(b.x-a.x); 43 } 44 double dist(point a,point b) 45 { 46 return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); 47 } 48 49 bool cmp(point a,point b) 50 { 51 if(dd(a.y,b.y)) 52 return xy(a.x,b.x); 53 return xy(a.y,b.y); 54 } 55 bool cmp1(point a,point b) 56 { 57 double len=crossProduct(c[0],a,b); 58 if(dd(len,0.0)) 59 return xy(dist(c[0],a),dist(c[0],b)); 60 return xy(len,0.0); 61 } 62 63 point stk[MAX]; 64 int top; 65 66 double dis() 67 { 68 double sum=0.0; 69 for(int i=0;i<top;i++) 70 { 71 sum+=dist(stk[i],stk[i+1]); 72 } 73 sum+=dist(stk[top],stk[0]); 74 return sum; 75 } 76 77 double Graham(int n) 78 { 79 sort(c,c+n,cmp); 80 sort(c+1,c+n,cmp1); 81 top=0; 82 stk[top++]=c[0]; 83 stk[top++]=c[1]; 84 stk[top++]=c[2]; 85 top--; 86 for(int i=3;i<n;i++) 87 { 88 while(1) 89 { 90 point a,b; 91 a=stk[top]; 92 b=stk[top-1]; 93 if(xyd(crossProduct(a,b,c[i]),0.0)) 94 { 95 top--; 96 } 97 else 98 break; 99 } 100 stk[++top]=c[i]; 101 } 102 return dis(); 103 } 104 105 int main() 106 { 107 int i,j,k,t; 108 int n,m; 109 while(scanf("%d",&n)!=EOF&&n) 110 { 111 for(i=0;i<n;i++) 112 { 113 scanf("%lf%lf",&c[i].x,&c[i].y); 114 } 115 if(n==1) 116 { 117 printf("0.00\n"); 118 //continue; 119 } 120 else if(n==2) 121 { 122 printf("%.2lf\n",2*dist(c[0],c[1])); 123 //continue; 124 } 125 else 126 { 127 printf("%.2lf\n",Graham(n)); 128 } 129 } 130 return 0; 131 }
zoj 1453 Surround the Trees(凸包求周长),布布扣,bubuko.com
zoj 1453 Surround the Trees(凸包求周长)
标签:style blog http color os strong io width
原文地址:http://www.cnblogs.com/ccccnzb/p/3868331.html