Wall
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 26246 | Accepted: 8745 |
Description
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.
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.
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
结果四舍五入就可以了
Source
题意:
给出N个凸多边形城堡的顶点以及城堡距围墙最近距离L
问若建造围墙 最短总距离为多少
代码:
#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iomanip>
#define INF 0x3f3f3f3f
#define pi 3.141592654
using namespace std;
typedef struct
{
int x,y;
}node;
int N,L;
node *s;
int distsquare(node a,node b) //距离的平方
{
return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}
double dist(node a,node b) //计算距离
{
return sqrt((double)(distsquare(a,b)));
}
int make(int a,int b,int c,int d) //叉积的计算
{
return a*d-b*c;
}
int cross(node a,node b,node c,node d)
{
return make(b.x-a.x,b.y-a.y,d.x-c.x,d.y-c.y);
}
int cmp(const void *a,const void *b) //比较函数
{ //先比较极角,极角由小到大
node *c=(node *)a,*d=(node *)b; //极角若相等,即在一条直线上时,比较与s点(最左下点)的距离,距离由小到大
int tmp=cross(*s,*c,*s,*d);
if(tmp>0) return -1;
else if(tmp==0) return dist(*d,*s)-dist(*c,*s);
else return 1;
}
int main()
{
while(scanf("%d%d",&N,&L)!=EOF)
{
node *point=new node[N+1];
int min_x=INF;
int min_point;
for(int i=1;i<=N;i++)
{
scanf("%d%d",&point[i].x,&point[i].y);
if(min_x>point[i].x)
{
min_x=point[i].x;
min_point=i;
}
else if(min_x==point[i].x)
{
if(point[min_point].y>point[i].y)
min_point=i;
}
}
point[0]=point[N]; //这三步为将前面找到的最左下的点放在point数组最后,为后面qsort做准备
point[N]=point[min_point]; //qsort中将找到的最左下的点作为参考点排序
point[min_point]=point[0];
s=&point[N]; //存储极坐标原点
qsort(point+1,N,sizeof(node),cmp);
int *bag=new int [N+2];
bag[1]=N;
bag[2]=1;
int cnt=2;
for(int i=2;i<=N;) //过滤选择凸包上的点
{
if(cross(point[bag[cnt-1]],point[bag[cnt]],point[bag[cnt]],point[i])>=0)
bag[++cnt]=i++;
else cnt--;
}
double min_len=0;
for(int i=1;i<cnt;i++)
min_len+=dist(point[bag[i]],point[bag[i+1]]);
min_len+=2*pi*L;
printf("%.0f\n",min_len);
delete point;
delete bag;
}
return 0;
}
思路:
1、构造凸包的方法貌似叫做GrahamScan Algorithm
2、最短总距离=城堡顶点坐标构成的散点集的凸包总边长 + 半径为L的圆周长