TZOJ 1471 Wall (凸包)

描述

Once upon a time there was a greedy King who ordered his chief Architect to build a wall around the King's castle. The King was so greedy, that he would not listen to his Architect's proposals to build a beautiful brick wall with a perfect shape and nice tall towers. Instead, he ordered to build the wall around the whole castle using the least amount of stone and labor, but demanded that the wall should not come closer to the castle than a certain distance. If the King finds that the Architect has used more resources to build the wall than it was absolutely necessary to satisfy those requirements, then the Architect will loose his head. Moreover, he demanded Architect to introduce at once a plan of the wall listing the exact amount of resources that are needed to build the wall.


Your task is to help poor Architect to save his head, by writing a program that will find the minimum possible length of the wall that he could build around the castle to satisfy King's requirements.
The task is somewhat simplified by the fact, that the King's castle has a polygonal shape and is situated on a flat ground. The Architect has already established a Cartesian coordinate system and has precisely measured the coordinates of all castle's vertices in feet.

输入

The first line of the input file contains two integer numbers N and L separated by a space. N (3 <= N <= 1000) is the number of vertices in the King's castle, and L (1 <= L <= 1000) is the minimal number of feet that King allows for the wall to come close to the castle.
Next N lines describe coordinates of castle's vertices in a clockwise order. Each line contains two integer numbers Xi and Yi separated by a space (-10000 <= Xi, Yi <= 10000) that represent the coordinates of ith vertex. All vertices are different and the sides of the castle do not intersect anywhere except for vertices.

输出

Write to the output file the single number that represents the minimal possible length of the wall in feet that could be built around the castle to satisfy King's requirements. You must present the integer number of feet to the King, because the floating numbers are not invented yet. However, you must round the result in such a way, that it is accurate to 8 inches (1 foot is equal to 12 inches), since the King will not tolerate larger error in the estimates.

样例输入

9 100
200 400
300 400
300 300
400 300
400 400
500 400
500 200
350 200
200 200

样例输出

1628

题意

给你n个点的坐标和L,要求找到包含这n个点的凸多边形,然后盖围墙,同时围墙与凸多边形上的点距离>=L,求这个围墙的周长。

思路

通过凸包求凸多边形的周长,因为要求围墙到城堡的最小距离为L,则围墙的最小总长度=凸包周长+一个以L为半径的圆周长。

代码

#include<bits/stdc++.h>
#define pi acos(-1.0)
using namespace std;
const int N =1010;
struct node
{
	int x,y;
}a[N],stackk[N];//存放输入进去的点和凸包上的点(利用模拟栈存储)
int xx,yy;//记录基点位置
bool cmp1(node a,node b)
{
    //找到基点,纵坐标最小的点一定在凸包上,纵坐标相等的情况下选横坐标小的
	if(a.y!=b.y)
	return a.y<b.y;
	return a.x<b.x;
}
bool cmp2(node a,node b)
{
    //atan2(a,b)是4象限反正切,它的取值不仅取决于a/b的atan值,还取决于点 (b, a) 落入哪个象限,这里指相对于基点的象限以及距离
	if(atan2(a.y-yy,a.x-xx)!=atan2(b.y-yy,b.x-xx))
	return atan2(a.y-yy,a.x-xx)< atan2(b.y-yy,b.x-xx);
	return a.x<b.x;
}
int cross(node a,node b,node c)
{
	return (b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x);//计算叉积
}
double dis(node a,node b)//计算两点间距离即边的长度
{
	return sqrt((a.x-b.x)*(a.x-b.x)*1.0+(a.y-b.y)*(a.y-b.y)*1.0);
}
int main()
{
	int n,l;
	while(~scanf("%d%d",&n,&l))
	{
		for(int i=0;i<n;i++)
		scanf("%d%d",&a[i].x,&a[i].y);
		memset(stackk,0,sizeof(stackk));
		sort(a,a+n,cmp1);
		stackk[0]=a[0];//基点入栈
		xx=stackk[0].x;
		yy=stackk[0].y;
		sort(a+1,a+n,cmp2);//角排序
		stackk[1]=a[1];
		int top=1;
		for(int i=2;i<n;i++)
		{
			while(i>=1 && cross(stackk[top-1],stackk[top],a[i])<0)
			top--;
			stackk[++top]=a[i];//对栈顶两元素向量,判断下一个元素是否在左边,在左边,下一个元素入栈,不在左边下一个元素出栈,若栈内元素不足两个,入栈。
		}
		double ans=0;
		for(int i=1;i<=top;i++)
		ans+=dis(stackk[i-1],stackk[i]);
		ans+=dis(stackk[0],stackk[top]);//计算周长
		ans+=2*pi*l;
		printf("%d\n",(int)(ans+0.5));//四舍五入
	}
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值