• 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): 6812    Accepted Submission(s): 2594


    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
     
    Recommend
    Ignatius.L   |   We have carefully selected several similar problems for you:  2108 2150 1558 1374 1391 
     
      计算几何:求凸包周长
      题意
      给你n棵树的坐标,你要用一根绳子包围所有的树,忽略树的半径和高度,求这根绳子的最短长度。
      思路
      这是求计算几何中的凸包周长。
      注意
      n=1或n=2的情况需要特殊考虑。
      代码
     1 #include <iostream>
     2 #include <iomanip>
     3 #include <cmath>
     4 using namespace std;
     5 
     6 struct Point{
     7     double x,y;
     8 }p[105],pl[105];
     9 double dis(Point p1,Point p2)
    10 {
    11     return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
    12 }
    13 double xmulti(Point p1,Point p2,Point p0)    //求p1p0和p2p0的叉积,如果大于0,则p1在p2的顺时针方向
    14 {
    15     return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
    16 }
    17 double graham(Point p[],int n)    //点集和点的个数 
    18 {
    19     int pl[105];
    20     //找到纵坐标(y)最小的那个点,作第一个点 
    21     int i;
    22     int t = 1;
    23     for(i=1;i<=n;i++)
    24         if(p[i].y < p[t].y)
    25             t = i;
    26     pl[1] = t;
    27     //顺时针找到凸包点的顺序,记录在 int pl[]
    28     int num = 1;    //凸包点的数量
    29     do{    //已确定凸包上num个点 
    30         num++; //该确定第 num+1 个点了
    31         t = pl[num-1]+1;
    32         if(t>n) t = 1;
    33         for(int i=1;i<=n;i++){    //核心代码。根据叉积确定凸包下一个点。 
    34             double x = xmulti(p[i],p[t],p[pl[num-1]]);
    35             if(x<0) t = i;
    36         }
    37         pl[num] = t;
    38     } while(pl[num]!=pl[1]);
    39     //计算凸包周长 
    40     double sum = 0;
    41     for(i=1;i<num;i++)
    42         sum += dis(p[pl[i]],p[pl[i+1]]);
    43     return sum;
    44 }
    45 int main()
    46 {
    47     int n;
    48     while(cin>>n){
    49         if(n==0) break;
    50         int i;
    51         for(i=1;i<=n;i++)
    52             cin>>p[i].x>>p[i].y;
    53         if(n==1){
    54             cout<<0<<endl;
    55             continue;
    56         }
    57         if(n==2){
    58             cout<<setiosflags(ios::fixed)<<setprecision(2);
    59             cout<<dis(p[1],p[2])<<endl;
    60             continue;
    61         }
    62         cout<<setiosflags(ios::fixed)<<setprecision(2);
    63         cout<<graham(p,n)<<endl;
    64     }
    65     return 0;
    66 }

    Freecode : www.cnblogs.com/yym2013

  • 相关阅读:
    将自己的工作环境全面移植到C++最后一道工序:能用MFC制作简单的图形界面
    Finally, the working environment has been moved to C++
    统计方面的书籍【zz】
    zz sql 通配符以及转义字符用法
    转载学习并实现DES加密解密算法(三)
    【资源分享】2009版大陆汉语常用字.txt下载
    自己实现的C++Trim()
    nmake命令(windows下的makefile)
    c++对象内存模型【内存布局】
    UML类图关系(VPUML工具绘图)
  • 原文地址:https://www.cnblogs.com/yym2013/p/3634513.html
Copyright © 2020-2023  润新知