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): 4726    Accepted Submission(s): 1784


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
 
  
  
#include<stdio.h>
#include<math.h>
#include<cstdlib>
const int MAXN=109;
const double eps=1e-6;
struct point
{
    double x,y;
}p[MAXN],h[MAXN];//p数组用于存所有点,h数组用于存凸包点
double distance(point p1,point p2)
{
    return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));  
}//   求两点之间的距离
double multiply(point sp,point ep,point op)
{
      return ((sp.x-op.x)*(ep.y-op.y)-(ep.x-op.x)*(sp.y-op.y));  
}//判断sp,ep,op是否满足左转,op是向量起始点,sp与op是两向量另外的端点
int cmp(const void *a,const void *b)
{//按极角排序
    point *p1=(point *)a;
    point *p2=(point *)b;
    double t=multiply(*p2,*p1,p[0]);
    if(t>eps)
		return 1;
    else if(fabs(t)<=eps)
    {
		if(distance(p[0],*p1)>distance(p[0],*p2))
			return 1;
		else
			return -1;
    }
    else
		return -1;
}
void anglesort(point p[],int n)
{//找到最左下方的点
    int i,k=0;
    point temp;
    for(i=1;i<n;i++)
        if(p[i].x<p[k].x||((p[i].x==p[k].x)&&(p[i].y<p[k].y)))
            k=i;
    temp=p[0],p[0]=p[k],p[k]=temp;
    qsort(p+1,n-1,sizeof(point),cmp);//找到左下点后再将左下点作为中心进行极角排序,从第p[1]个元素开始,共n-1个元素
}
void Graham_scan(point p[],point ch[],int n,int &len)
{//建立凸包
    int i,top=2;
    anglesort(p,n);
    if(n<3)
	{
        for(i=0,len=n;i<n;i++)
			ch[i]=p[i];
        return;//终止函数
    }
    ch[0]=p[0],ch[1]=p[1],ch[2]=p[2];
    p[n]=p[0];
    for(i=3;i<=n;i++)
	{
        while(multiply(ch[top],p[i],ch[top-1])<=0)
			top--;
        ch[++top]=p[i];
    }
    len=top+1;
}
int main()
{
    int i,n,len;
    while(scanf("%d",&n)!=EOF&&n)
	{
        for(i=0;i<n;i++)
			scanf("%lf %lf",&p[i].x,&p[i].y);
        Graham_scan(p,h,n,len);//p为所有的点h为凸包点len为凸包点的个数
        double sum=0;
        for(i=1;i<len;i++)
        {
           sum+=distance(h[i],h[i-1]);
        }
        printf("%.2lf\n",sum);
	}
    return 0;  
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值