*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\n");continue;}
        if(n==2) {printf("%.2lf\n",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\n",ans);
    }
    return 0;
}

 

 

 

 

转载于:https://www.cnblogs.com/--ZHIYUAN/p/6080446.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值