poj Wall
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.
![](http://poj.org/images/1113/1113_1.gif)
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.
![](http://poj.org/images/1113/1113_1.gif)
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
这题考的就是凸包的创建,其他对就是没什么了,扫描来创建的,也有的用别的方法,这题有很多种方法,可惜只会这一种,呵呵
创建的时候,方向要选对,这是最关键的,不对的话,会导致很多郁闷的答案。View Code
#include"stdio.h"
#include"math.h"
#include"stdlib.h"
#define maxn 1004
#define eps 1e-8
#define pi 3.141592653
typedef struct node
{
double x,y;
}Tpoint ;
Tpoint tree[maxn];
int stacks[maxn*2],top;
double dix(Tpoint a,Tpoint b,Tpoint c)
{
return (a.x-c.x)*(b.y-c.y)-(b.x-c.x)*(a.y-c.y);
}
double dist(Tpoint a,Tpoint b)
{
double p,q;
p = a.x-b.x;
q = a.y-b.y;
//printf("%.2lf\n",p*p+q*q);
return p*p+q*q;
}
int cmp(const void *a,const void *b)
{
Tpoint *c = (Tpoint *)a,*d = (Tpoint *)b;
double k = dix(*c,*d,tree[0]);
if(fabs(k)<eps && dist(*c,tree[0])>dist(*d,tree[0]))return 1;
if(k<0 && fabs(k)>eps)return 1;
return -1;
}
void creat(int n)
{
int i,j,k,u=0;
Tpoint tmp;
for(i=1;i<n;i++)
{
if(tree[i].y<tree[u].y || (tree[i].y==tree[u].y && tree[i].x<tree[u].x))u = i;
}
tmp = tree[0];
tree[0] = tree[u];
tree[u] = tmp;
qsort(tree+1,n-1,sizeof(tree[0]),cmp);//for(i=0;i<n;i++)printf("%.2lf\n",tree[i].x);
stacks[0] = 0;
stacks[1] = 1;
//stacks[2] = 2;
top = 1;
for(i=2;i<n;i++)
{
while(top>0 && dix(tree[i],tree[stacks[top]],tree[stacks[top-1]])>=0)top--;
top++;
stacks[top] = i;
}
}
double ares(double ri)
{
int i,j,k;
double ans=0.0;//for(i=0;i<=top;i++)printf("stacks.x = %.2lf\n",tree[stacks[i]].x);
for(i=0;i<=top;i++)
{
ans = ans + sqrt(dist(tree[stacks[i]],tree[stacks[(i+1)%(top+1)]]));
}
//printf("ans = %.2lf\n",ans);
ans = ans + 2*pi*ri;
return ans;
}
int main()
{
int i,j,k;
double ri,len;
while(scanf("%d%lf",&k,&ri)==2)
{
for(i=0;i<k;i++)
{
scanf("%lf%lf",&tree[i].x,&tree[i].y);
}
creat(k);
len = ares(ri);
len += 0.5;
k = (int)len;
printf("%d\n",k);
}
return 0;
}