题目描述
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.
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.
This problem contains multiple test cases!
The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.
The output format consists of N output blocks. There is a blank line between output blocks.
Sample Input
1
9 100
200 400
300 400
300 300
400 300
400 400
500 400
500 200
350 200
200 200
Sample Output
1628
分析
经过分析,本题是让我们求包含了n个点的凸包的周长,外加一个半径为L的圆的周长的总长:
使用Graham扫描求出凸包,最后加上圆的周长。
Graham求凸包讲解
对于可以使用acos进行定义:
const double PI = acos(-1.0);
C++ 代码
#include<bits/stdc++.h>
using namespace std;
const int N = 1e3+10;
const double PI = acos(-1.0); //PI的表现方式
int n,L,T,stk[N],tt;
struct node{
int x,y;
bool operator<(const node &p) const{
if(y==p.y) return x<p.x;
return y<p.y;
}
}point[N],p0;
int cross(node p,node a,node b) //求叉积
{
return (a.x-p.x)*(b.y-p.y)-(b.x-p.x)*(a.y-p.y);
}
double dis(node a,node b) //求两点间的距离
{
return sqrt(1.0*(a.x-b.x)*(a.x-b.x)+1.0*(a.y-b.y)*(a.y-b.y));
}
bool cmp(node a,node b) //按极角大小排序函数
{
int t=cross(p0,a,b);
if(t>0) return 1;
if(t==0 && (dis(p0,a)-dis(p0,b))<=0) return 1;
return 0;
}
void graham(int n) //求出凸包的所有点集
{
tt=2; //栈初始长度为2
stk[1]=1;
stk[2]=2;
for(int i=3;i<=n;i++)
{
while(tt>=2 && cross(point[stk[tt-1]],point[stk[tt]],point[i])<=0)
{
tt--;
}
stk[++tt]=i;
}
}
int main()
{
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&L);
tt=0;
memset(point,0,sizeof point);
for(int i=1;i<=n;i++)
scanf("%d%d",&point[i].x,&point[i].y);
sort(point+1,point+1+n); //找到起始点p0
p0=point[1];
sort(point+2,point+1+n,cmp); //对p0以外的所有点按极角大小排序
graham(n);
double ans=0;
for(int i=2;i<=tt;i++) //计算凸包的总周长
ans+=dis(point[stk[i]],point[stk[i-1]]);
ans+=dis(point[stk[1]],point[stk[tt]]);
ans+=PI*2*L; //加上半径为L的圆的周长
printf("%.0lf\n",ans);
if(T!=0)puts("");
}
return 0;
}