zoukankan      html  css  js  c++  java
  • [计算几何][求解凸包]Surround the Trees

    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
     
    思路:Graham扫描法求解凸包——https://max.book118.com/html/2017/0409/99594646.shtm(讲得炒鸡生动!!)
    AC代码:
    #include <iostream>
    #include<cstdio>
    #include<algorithm>
    #include<cmath>
    using namespace std;
    int n;
    int top;
    double ans;
    
    struct Point{
      int x,y;
    }point[110],stk[110];
    
    double dis(Point a,Point b){//求两点距离
       return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
    }
    
    double cross(Point a,Point b,Point c){//向量ab与向量ac的叉积
       return (b.x-a.x)*(c.y-a.y)-(c.x-a.x)*(b.y-a.y);
    }
    
    void find_miny(){//找point[0]
       Point tmp=point[0];
       int flag=0;
       for(int i=1;i<n;i++){
         if(point[i].y<tmp.y||(point[i].y==tmp.y&&point[i].x<tmp.x)){
             tmp=point[i]; flag=i;
         }
       }
       if(flag) swap(point[0],point[flag]);
    }
    
    bool cmp(Point a,Point b){//点的排序
       double n=cross(point[0],a,b);
       if(n>0||(n==0&&dis(point[0],a)<dis(point[0],b))) return true;
       return false;
    }
    
    void Graham(){//Graham扫描法
       top=-1;
       stk[++top]=point[0]; stk[++top]=point[1];
       for(int i=2;i<n;i++){
          while(top&&cross(stk[top-1],stk[top],point[i])<0) top--;
          stk[++top]=point[i];
       }
    }
    
    void sum(){//求凸包的周长
       ans=0;
       stk[++top]=point[0];
       for(int i=0;i<top;i++){
         ans+=dis(stk[i],stk[i+1]);
       }
    }
    
    int main()
    {
        while(scanf("%d",&n)!=EOF&&n){
            for(int i=0;i<n;i++){
                scanf("%d%d",&point[i].x,&point[i].y);
            }
            if(n==1) {
                printf("0.00
    "); continue;
            }
            if(n==2) {
                printf("%.2f
    ",dis(point[0],point[1])); continue;
            }
            find_miny();
            sort(point+1,point+n,cmp);
            Graham();
            sum();
            printf("%.2f
    ",ans);
        }
        return 0;
    }
    转载请注明出处:https://www.cnblogs.com/lllxq/
  • 相关阅读:
    有点忙啊
    什么是协程
    HDU 1110 Equipment Box (判断一个大矩形里面能不能放小矩形)
    HDU 1155 Bungee Jumping(物理题,动能公式,弹性势能公式,重力势能公式)
    HDU 1210 Eddy's 洗牌问题(找规律,数学)
    HDU1214 圆桌会议(找规律,数学)
    HDU1215 七夕节(模拟 数学)
    HDU 1216 Assistance Required(暴力打表)
    HDU 1220 Cube(数学,找规律)
    HDU 1221 Rectangle and Circle(判断圆和矩形是不是相交)
  • 原文地址:https://www.cnblogs.com/lllxq/p/8983263.html
Copyright © 2011-2022 走看看