ACM-计算几何之Wall——poj1113

Wall

题目:http://poj.org/problem?id=1113

Time Limit: 1000MS Memory Limit: 10000K

Total Submissions: 28586 Accepted: 9556

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.
ACM-计算几何之Wall——poj1113_第1张图片
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

结果四舍五入就可以了


依旧是求凸包周长,最后再加一个圆的周长就OK,

圆的半径是所输入的第二个数。

恩,其实我是把求凸包周长的模板加上圆的周长,就直接A了。。

恩,结果是四舍五入的,其实输出的时候  %.0lf 就可以了。

题目中  round to是四舍五入的意思哟。。o(╯□╰)o。。。还好有汉语提示


/*
Author:Tree
From: http://blog.csdn.net/lttree
Wall
poj 1113
凸包周长
*/
#include <stdio.h>
#include <math.h>
#include <algorithm>
using namespace std;
#define pi 3.1415926
struct point
{
    double x,y;
}pnt[10001],res[10001];
// 两向量叉积
double cross( point sp,point ep,point op)
{
    return (sp.x-op.x)*(ep.y-op.y)-(ep.x-op.x)*(sp.y-op.y);
}
// sort数组排序,比较的<号重载
bool operator < (const point &l,const point &r)
{
    return l.y<r.y || (l.y==r.y && l.x<r.x) ;
}
// 求两点间几何距离
double dis(point a,point b)
{
    return sqrt( (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y) );
}
int Graham( int n )
{
    int i,len,top=1;
    // 先对所有点按照极角排序
    // 纵坐标最小的排在前面,纵坐标相同,横坐标小的排在前面
    sort(pnt,pnt+n);
    // 判断点的个数是否大于2(所给的点能否构成凸包)
    if( n==0 )  return 0;res[0] = pnt[0];
    if( n==1 )  return 1;res[1] = pnt[1];
    if( n==2 )  return 2;res[2] = pnt[2];
    // 用叉积来判断后面的点是否为凸包中的点
    for( i=2;i<n;++i )
    {
        while( top && cross(pnt[i],res[top],res[top-1])>=0 )   top--;
        res[++top] = pnt[i];
    }
    len = top; res[++top] = pnt[n-2];
    // 判断最后三个点
    for(i=n-3;i>=0;--i)
    {
        while( top!=len && cross(pnt[i],res[top],res[top-1])>=0 )  top--;
        res[++top] = pnt[i];
    }
    return top;
}
int main()
{
    int n,i,len,r;
    double sum;
    while(scanf("%d%d",&n,&r)!=EOF)
    {
        for(i=0;i<n;++i)
            scanf("%lf%lf",&pnt[i].x,&pnt[i].y);
        // len为凸包数组内点的个数
        len=Graham(n);
        // 判断n为0,1,2的情况(无法构造凸包的情况)
        if(len==0 || len==1)  {printf("0\n");continue;}
        if(len==2)  {printf("%.2lf\n",dis(res[0],res[1]));continue;}

        // 求周长
        sum=0;
        for(i=0;i<len;++i)
            sum=sum+dis(res[i],res[i+1]);
        sum=sum+2*pi*r;
        printf("%.0lf\n",sum);
    }
    return 0;
}


你可能感兴趣的:(ACM,计算几何,Wall,POJ1113,求凸包周长)