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): 12460    Accepted Submission(s): 4822


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
 
 
12 7 
24 9 
30 5 
41 9 
80 7 
50 87 
22 9 
45 1 
50 7 
0
 

Sample Output
 
 
243.06
Graham Scan算法是一种十分简单高效的二维凸包算法,能够在O(nlogn)的时间内找到凸包。这里用的是坐标排序 

可以当模板。

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<algorithm>
using namespace std;
int n;
struct node{
	double x,y;
}point[105];//保存输入点的坐标 
int convex[105];//保存选择的凸包点 
int corss(node a,node b,node c)//向量叉乘 
{
	return (a.x-b.x)*(c.y-b.y)-(a.y-b.y)*(c.x-b.x);
}
int cmp(node a,node b)//坐标排序 
{
	if(a.x==b.x) return a.y < b.y ;
	else return a.x < b.x; 
}
double len(node a,node b)//周长计算 
{
	return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
void getConvexHull()//凸包 
{
	sort(point,point+n,cmp);
	int temp,ctotal;
	int total=0;
	if(n==2)
	{
		printf("%.2lf\n",len(point[0],point[1]));
		return;
	}
	for(int i=0;i<n;i++)
	{
		while(total>1&&corss(point[i],point[convex[total-1]],point[convex[total-2]])<=0)
			total--;
			convex[total++]=i;
	}
	temp=total;
	for(int i=n-2;i>=0;i--)
	{
		while(total>temp&&corss(point[i],point[convex[total-1]],point[convex[total-2]])<=0)
			total--;
			convex[total++]=i;
	}
	ctotal=total;//凸包点总个数 
	double sum=0;
	for(int i=0;i<ctotal-1;i++)
	{
		sum+=len(point[convex[i]],point[convex[i+1]]);
	}
	sum+=len(point[convex[0]],point[convex[ctotal-1]]);
	printf("%.2lf\n",sum);
	return;
}
int main()
{
	while(~scanf("%d",&n)&&n)
	{
		for(int i=0;i<n;i++)
		{
			scanf("%lf%lf",&point[i].x ,&point[i].y);
		}
		getConvexHull();
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值