zoukankan      html  css  js  c++  java
  • *HDU 1392 计算几何

    Surround the Trees

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


    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
     
    题意:
    求以上n个点的凸包的周长
    讲解很详细的博客:http://www.cnblogs.com/jbelial/archive/2011/08/05/2128625.html
    代码:
    //求凸包的模板题Graham扫描法。
    //详解《算法导论》604页
    //极角排序先比较象限再比较叉积。
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<cmath>
    using namespace std;
    const int INF=0x7fffffff;
    int top,n,q[109];//q用于保存组成凸包的点
    struct Node { double x,y; }node[109];
    double dis(Node p1,Node p2)
    {
        return sqrt((p2.x-p1.x)*(p2.x-p1.x)+(p2.y-p1.y)*(p2.y-p1.y));
    }
    int Qd(Node p)//返回点相对于p[0]点所在的象限
    {
        p.x-=node[0].x;
        p.y-=node[0].y;
        if(p.x>=0&&p.y>=0) return 1;
        else if(p.x<=0&&p.y>0) return 2;
        else if(p.x<0&&p.y<=0) return 3;
        else if(p.x>=0&&p.y<0) return 4;
    }
    double chaji(Node p0,Node p1,Node p2)//叉积
    {
        return ((p1.x-p0.x)*(p2.y-p0.y)-(p1.y-p0.y)*(p2.x-p0.x));
    }
    bool cmp(Node p1,Node p2)
    {
        int Q1=Qd(p1),Q2=Qd(p2);
        if(Q1==Q2){
            double tmp=chaji(node[0],p1,p2);
            if(tmp>0) return 1;//tem>0说明向量p1p0在向量p2p0的顺时针方向即p1p0相对于p0的极角小于p2p0的
            else if(tmp<0) return 0;
            else return fabs(p1.x)<fabs(p2.x);
        }
        else return Q1<Q2;
    }
    void tubao()
    {
        top=0;
        q[++top]=0;
        q[++top]=1;
        for(int i=2;i<=n;i++){
            while(top>1&&chaji(node[q[top-1]],node[q[top]],node[i])<=0)
                top--;
            q[++top]=i;
        }
    }
    int main()
    {
        while(scanf("%d",&n)&&n){
            double min_x=INF,min_y=INF;
            int min_i=0;
            for(int i=0;i<n;i++){
                scanf("%lf%lf",&node[i].x,&node[i].y);
                if(min_y>node[i].y){
                    min_y=node[i].y;
                    min_x=node[i].x;
                    min_i=i;
                }else if(min_y==node[i].y&&min_x<node[i].x){
                    min_x=node[i].x;
                    min_i=i;
                }
            }
            swap(node[min_i],node[0]);
            if(n==1) {printf("0.00
    ");continue;}
            if(n==2) {printf("%.2lf
    ",dis(node[0],node[1]));continue;}//计算凸包的点数必须多于2
            sort(node+1,node+n,cmp);
            node[n].x=node[0].x;node[n].y=node[0].y;//形成闭合的凸包
            tubao();
            double ans=0;
            for(int i=1;i<top;i++){
                ans+=dis(node[q[i]],node[q[i+1]]);
            }
            printf("%.2lf
    ",ans);
        }
        return 0;
    }
  • 相关阅读:
    CentOS7使用集群同步脚本对文件同步分发
    CentOS7安装jdk1.8
    CentOS7+CDH5.12.1集群搭建
    nginx通配符
    Nginx反向代理及配置
    一些好玩有用的书签
    linux操作小技巧锦集
    系统压测结果对比:tomcat/thinkphp/swoole/php-fpm/apache
    python修改linux日志(logtamper.py)
    【原创】给定随机数的取值范围(最小值、最大值),且要求多次取得的随机数最后的结果有一个固定的平均值
  • 原文地址:https://www.cnblogs.com/--ZHIYUAN/p/6080446.html
Copyright © 2011-2022 走看看