天天看点

POJ - 1113 Wall (Andrew算法+凸包)

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. 
POJ - 1113 Wall (Andrew算法+凸包)

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.

Input

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.

Output

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.

Sample Input
9 100
200 400
300 400
300 300
400 300
400 400
500 400
500 200
350 200
200 200
           
Sample Output
1628
           

Hint

结果四舍五入就可以了

题目大意,给你城堡的各个点的坐标,求距离城堡围墙L远处的最短隔墙长度

解题思路:使用Andrew算法求出凸包 ,隔墙的长度的最小值= 城堡顶点坐标构成的散点集的凸包总边长 + 半径为L的圆周长

下面是大神的公式推导:

假如顺时针给出四个点A、B、C、D。组成了凸四边形ABCD。我们不妨过A点作AE垂直于AB,同时过A点再作AF垂直于AD,过B点作BG、BH分别垂直于AB、BC。连结EG,垂线段的长度为L,过A点以AE为半径作一段弧连到AF,同理,使GH成为一段弧。此时EG=AB(边),AB段城墙的最小值为EF+弧EF+弧GH=AB+弧EF+弧GH。对所有点进行同样的操作后,可知城墙的最小值=四边形的周长+相应顶点的弧长(半径都为L)之和。

下面证明这些顶点弧长组成一个圆。依然以前面的四边形为例。A、B、C、D四顶点各成周角,总和为360*4=1440度,四边形内角和为360度,每个顶点作两条垂线,总角度为4*2*90=720度,所以总圆周角为1440-360-720=360度,刚好组成圆。

所以四边形ABCD的围墙最短= 四边形的周长+圆周长。

推广到任意多边形,用同样的方法,城墙最短=凸包的周长 + 以L为半径的圆的周长。

首先,我们得出城墙最短=凸包的周长 + 相应顶点的弧长(半径都为L)之和。

再证明 相应顶点的弧长(半径都为L)之和=以L为半径的圆的周长。

事实上,设凸包顶点为n,n个顶点组成n个周角,角度为360*n=2*180*n,凸包的内角和为180*(n-2),作了2*n条垂线,和为2*n*90=180*n,所以总圆周角为2*180*n-180*(n-2)-180*n=360,组成圆。

Andrew算法模板:

struct point{
	int x,y;
	
}ch[maxn],p[maxn];

int Andrew(point *p,int n,point *ch)//Andrew算法求凸包 
{
	sort(p,p+n,cmp);
	int m=0;
	for(int i=0;i<n;i++)
	{
		while(m>1&&det(ch[m-2],ch[m-1],p[i])<0)
			m--;
		ch[m++]=p[i];//ch保存凸包的坐标点 
	}
	int k=m;
	for(int i=n-2;i>=0;i--)
	{
		while(m>k&&det(ch[m-2],ch[m-1],p[i])<0)
			m--;
		ch[m++]=p[i];
	}
	if(n>1)
		m--;
	return m;
}
           

AC代码:

#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
const int maxn=1e3+10;
#define pi acos(-1.0)
struct point{
	int x,y;
	
}ch[maxn],p[maxn];
bool cmp(point a,point b)
{
	if(a.x==b.x)
		return a.y<b.y;
	else
		return a.x<b.x;
}
int det(point a,point b,point c)//叉乘 
{
	return (b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x); 
} 
int Andrew(point *p,int n,point *ch)//Andrew算法求凸包 
{
	sort(p,p+n,cmp);
	int m=0;
	for(int i=0;i<n;i++)
	{
		while(m>1&&det(ch[m-2],ch[m-1],p[i])<0)
			m--;
		ch[m++]=p[i];//ch保存凸包的坐标点 
	}
	int k=m;
	for(int i=n-2;i>=0;i--)
	{
		while(m>k&&det(ch[m-2],ch[m-1],p[i])<0)
			m--;
		ch[m++]=p[i];
	}
	if(n>1)
		m--;
	return m;
}
double dis(point a,point b)//两点间距离 
{
	return sqrt(1.0*(b.x-a.x)*(b.x-a.x)+(b.y-a.y)*(b.y-a.y));
}
int main()
{
	int n,L;
	double sum=0;
	cin>>n>>L;
	for(int i=0;i<n;i++)
		cin>>p[i].x>>p[i].y;
	int m=Andrew(p,n,ch);
	for(int i=0;i<m-1;i++)
	{
		sum+=dis(ch[i],ch[i+1]);
	}
	sum+=dis(ch[m-1],ch[0])+2*pi*L;
	printf("%.lf\n",sum);
	return 0; 
}