设为首页 加入收藏

TOP

HDU1392:Surround the Trees(凸包问题)
2015-07-24 05:32:10 来源: 作者: 【 】 浏览:7
Tags:HDU1392 Surround the Trees 问题

Surround the Trees


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


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 Z??http://www.2cto.com/kf/ware/vc/" target="_blank" class="keylink">vciBtb3JlIGRhdGEgc2V0cy4gQXQgZmlyc3QgbGluZSBvZiBlYWNoIGlucHV0IGRhdGEgc2V0IGlzIG51bWJlciBvZiB0cmVlcyBpbiB0aGlzIGRhdGEgc2V0LCBpdCBpcyBmb2xsb3dlZCBieSBzZXJpZXMgb2YgY29vcmRpbmF0ZXMgb2YgdGhlIHRyZWVzLiBFYWNoIGNvb3JkaW5hdGUgaXMgYSBwb3NpdGl2ZSBpbnRlZ2VyIHBhaXIsIGFuZCBlYWNoIGludGVnZXIKIGlzIGxlc3MgdGhhbiAzMjc2Ny4gRWFjaCBwYWlyIGlzIHNlcGFyYXRlZCBieSBibGFuay48YnI+Cjxicj4KWmVybyBhdCBsaW5lIGZvciBudW1iZXIgb2YgdHJlZXMgdGVybWluYXRlcyB0aGUgaW5wdXQgZm9yIHlvdXIgcHJvZ3JhbS48YnI+CgogCjxicj4KT3V0cHV0ClRoZSBtaW5pbWFsIGxlbmd0aCBvZiB0aGUgcm9wZS4gVGhlIHByZWNpc2lvbiBzaG91bGQgYmUgMTBeLTIuPGJyPgoKIAo8YnI+ClNhbXBsZSBJbnB1dAoKPHByZSBjbGFzcz0="brush:java;">9 12 7 24 9 30 5 41 9 80 7 50 87 22 9 45 1 50 7 0
Sample Output
243.06


题意是求将所有点围住的那个面积的最小周长。。但是要注意当只有一个点时,也就输出0.00,当只有两个点时。。也就是两点间的距离。。

这是凸包问题的入门题。。。(Orz) 用的是刘汝佳大白上的Andrew算法。。看他的代码实现。。简直丧心病狂。。Orz 。。搞了好久的时间。。智商完全不够用。。

好吧。。因为是今天刚刚接触。。所以一天也就弄了这么一道题。。5555555.。。泪流满面。。。




 

#include
  
   
#include
   
     #include
    
      #include
     
       #include
      
        #include
       
         #include
        
          #include
         
           #define f1(i, n) for(int i=0; i
          
           p[j+1].x) { temp = p[j]; p[j] = p[j+1]; p[j+1] = temp; } if(p[j].x==p[j+1].x && p[j].y>p[j+1].y) { temp = p[j]; p[j] = p[j+1]; p[j+1] = temp; } } } int cross(int x1, int y1, int x2, int y2) //看P[i]是否是在其内部。。 { if(x1*y2-x2*y1<=0) //叉积小于0,说明p[i]在当前前进方向的右边,因此需要从凸包中删除c[m-1],c[m-2] return 0; else return 1; } int convexhull(Point *p, Point *c, int n) { int i,m=0,k; f1(i, n) //下凸包 { while(m>1 && !cross(c[m-2].x-c[m-1].x,c[m-2].y-c[m-1].y,c[m-1].x-p[i].x,c[m-1].y-p[i].y)) m--; c[m++]=p[i]; } k=m; for(i=n-2; i>=0; i--) //求上凸包 { while(m>k && !cross(c[m-2].x-c[m-1].x,c[m-2].y-c[m-1].y,c[m-1].x-p[i].x,c[m-1].y-p[i].y)) m--; c[m++]=p[i]; } if(n>1) m--; return m; } double dis(Point a, Point b) //求两个凸包点之间的长度。。 { return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); } int main() { Point a[105], p[105]; int n, i, m; double lenth; while(scanf("%d",&n) &&n) { f1(i, n) scanf("%lf %lf",&a[i].x, &a[i].y); if(n==1) { printf("0.00\n"); continue; } else if(n==2) { printf("%.2lf\n", dis(a[0], a[1])); continue; } sort(a, n); m=convexhull(a, p, n); lenth = 0; f2(i, m) lenth+=dis(p[i],p[i-1]); printf("%.2lf\n",lenth); } return 0; } 
          
         
        
       
      
     
    
   
  


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇编程算法 - 圆圈中最后剩下的数字.. 下一篇POJ 2955 Brackets

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: