Hdu1348凸包graham——scan法

题意:一个国王要求建筑师修一堵墙,围绕他的城堡,且墙与城堡之间的距离总不小于L输入节点和L求周长
解析:此题是求凸包但是有变形,因为多了一个L可以这样想,墙除了在顶点处的延生出去是个弧形意外其他的地方求只是延生出去L但是长度不变,剩下的就是求弧长了,接下来可以想象其实所有的弧长加起来是个圆心角360的圆弧,画个长方形类比下就知道了



#include<iostream>
#include<algorithm>
#include<cmath>
#define MAX 1000
using namespace std;
struct points
{
	int x,y;
};
points p[MAX];
int n,l,res[MAX],top;
bool cmp(const points &a,const points &b)
{
	if(a.y==b.y)return a.x<b.x;
	return a.y<b.y;
	
}
bool mult(points sp,points ep,points op)
{
	return (sp.x-op.x)*(ep.y-op.y)>=(ep.x-op.x)*(sp.y-op.y);
}
void graham()
{
	int i,len;
	top=1;
	sort(p,p+n,cmp);
	if(n==0)return; res[0]=0;
	if(n==1)return; res[1]=1;
	if(n==2)return; res[2]=2;
	for(i=2;i<n;i++)
	{
		while(top && mult(p[i],p[res[top]],p[res[top-1]]))
			top--;
			res[++top]=i;
	}
	len=top;
	res[++top]=n-2;
	for(i=n-3;i>=0;i--)
	{
		while(top!=len && mult(p[i],p[res[top]],p[res[top-1]]))
			top--;
			res[++top]=i;
	}
}
int main()
{
	int tt;
	while(scanf("%d",&tt)!=EOF)
	{
		while(tt--)
		{
			scanf("%d%d",&n,&l);
			{
				for(int i=0;i<n;i++)
					scanf("%d%d",&p[i].x,&p[i].y);
				graham();
				double re=4*acos(0.0)*l;
				for(int i=0;i<top;i++)
					re+=sqrt((p[res[i]].x-p[res[i+1]].x)*(p[res[i]].x-p[res[i+1]].x)*1.0
							+(p[res[i]].y-p[res[i+1]].y)*(p[res[i]].y-p[res[i+1]].y)*1.0);
				printf("%.0lf\n",re);
				if(tt)
				printf("\n");
			}
		}
	}
	return 0;	
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值