标签:des c style class blog code
Time Limit: 2000/1000 MS
(Java/Others) Memory Limit: 65536/32768 K
(Java/Others)
Total Submission(s): 7043 Accepted
Submission(s): 2688
凸包入门:
1 //78MS 248K 1553 B C++ 2 #include<stdio.h> 3 #include<stdlib.h> 4 #include<math.h> 5 #define N 105 6 struct node{ 7 double x,y; 8 }p[N],stack[N]; 9 double dist(node a,node b) //两点距离 10 { 11 return sqrt((a.y-b.y)*(a.y-b.y)+(a.x-b.x)*(a.x-b.x)); 12 } 13 double crossprod(node a,node b,node c) //计算叉积 14 { 15 return (b.x-a.x)*(c.y-a.y)-(c.x-a.x)*(b.y-a.y); 16 } 17 int cmp(const void*a,const void*b) //按要求排序 18 { 19 node c=*(node*)a; 20 node d=*(node*)b; 21 double k=crossprod(p[0],c,d); 22 if(k<0 || !k && dist(p[0],c)>dist(p[0],d)) 23 return 1; 24 return -1; 25 } 26 double Graham(int n) //Graham扫描法 27 { 28 for(int i=1;i<n;i++) 29 if(p[i].x<p[0].x || p[i].x==p[0].x && p[i].y<p[0].y){ 30 node temp=p[0]; 31 p[0]=p[i]; 32 p[i]=temp; 33 } 34 qsort(p+1,n-1,sizeof(p[0]),cmp); 35 p[n]=p[0]; 36 for(int i=0;i<3;i++) 37 stack[i]=p[i]; 38 int top=2; 39 for(int i=3;i<n;i++){ 40 while(crossprod(stack[top-1],stack[top],p[i])<=0 && top>=2) 41 top--; 42 stack[++top]=p[i]; 43 } 44 double ans=dist(stack[0],stack[top]); 45 for(int i=0;i<top;i++) 46 ans+=dist(stack[i],stack[i+1]); 47 return ans; 48 } 49 int main(void) 50 { 51 int n; 52 while(scanf("%d",&n),n) 53 { 54 for(int i=0;i<n;i++) 55 scanf("%lf%lf",&p[i].x,&p[i].y); 56 if(n==1){ 57 puts("0.00");continue; 58 } 59 if(n==2){ 60 printf("%.2lf\n",dist(p[0],p[1])); 61 continue; 62 } 63 printf("%.2lf\n",Graham(n)); 64 } 65 return 0; 66 }
hdu 1392 Surround the Trees (凸包),布布扣,bubuko.com
hdu 1392 Surround the Trees (凸包)
标签:des c style class blog code
原文地址:http://www.cnblogs.com/GO-NO-1/p/3765139.html