POJ - 1113 Wall (凸包 Graham掃描算法 | 捲包裹算法)

Wall

 

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.

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

結果四捨五入就可以了

 

題目鏈接:http://poj.org/problem?id=1113

題目大意:給出多邊形的n個頂點,求一個圖形的最小周長,要求圖形能包含多邊形的所有頂點,並且圖形與多邊形所有頂點的距離都不小於 l 

思路:就是凸包的周長 + 以 l 爲半徑的圓的周長  ( Graham掃描算法求凸包)

Graham掃描算法詳解

代碼:

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
using namespace std;
const int N=100005;
const double pi=acos(-1.0);
int xx,yy;
int n,k;
struct node
{
    int x,y;
} e[N],s[N];
bool cmp(node a,node b)//找到左下角
{
    if(a.y==b.y) return a.x<b.x;
    return a.y<b.y;
}
int cross(node a,node b,node c)//叉積
{
    return (b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x);
}
double dist(node a,node b)//距離
{
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
bool cmp1(node a,node b)//極角排序
{
    int mm=cross(e[0],a,b);
    if(mm>0) return 1;
    else if(mm==0&&dist(e[0],a)-dist(e[0],b)<=0)
        return 1;
    else return 0;
}
bool cmp2(node a,node b)//極角排序(較快)
{
    if(atan2(a.y-yy,a.x-xx)!=atan2(b.y-yy,b.x-xx))
        return (atan2(a.y-yy,a.x-xx))<(atan2(b.y-yy,b.x-xx));
    return a.x<b.x;
}
void graham()
{
    sort(e,e+n,cmp);
    xx=e[0].x,yy=e[0].y;
    sort(e+1,e+n,cmp1);
    int tot=1;
    s[0]=e[0],s[1]=e[1];
    for(int i=2; i<n; i++)
    {
        while(tot&&cross(s[tot-1],s[tot],e[i])<=0)
            tot--;
        s[++tot]=e[i];
    }
    double ans=2.0*pi*k;
    for(int i=0; i<tot; i++)
        ans+=dist(s[i],s[i+1]);
    ans+=dist(s[0],s[tot]);
    printf("%d\n",(int)(ans+0.5));
}
int main()
{
    while(~scanf("%d%d",&n,&k))
    {
        for(int i=0; i<n; i++)
            scanf("%d%d",&e[i].x,&e[i].y);
        graham();
    }
    return 0;
}

 

代碼:

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
using namespace std;
const double pi=acos(-1.0);
const int N=100005;
struct node
{
    int x,y;
}e[N],s[N];
int n,k,tot;
bool cmp(node a,node b)
{
    if(a.y==b.y) return a.x<b.x;
    return a.y<b.y;
}
int cross(node a,node b,node c)
{
    return (b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x);
}
double dis(node a,node b)
{
    return sqrt(1.0*(a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
void convex()
{
    sort(e,e+n,cmp);
    tot=1;
    s[0]=e[0];s[1]=e[1];
    for(int i=2;i<n;i++)
    {
        while(tot&&cross(s[tot-1],s[tot],e[i])<=0)
            tot--;
        s[++tot]=e[i];
    }
    int len=tot;
    for(int i=n-2;i>=0;i--)
    {
        while(tot>len&&cross(s[tot-1],s[tot],e[i])<=0)
            tot--;
        s[++tot]=e[i];
    }
}
int main()
{
    scanf("%d%d",&n,&k);
    for(int i=0;i<n;i++)
        scanf("%d%d",&e[i].x,&e[i].y);
    convex();
    double res=2.0*pi*k;
    for(int i=0;i<tot;i++)
        res+=dis(s[i],s[i+1]);
    printf("%d\n",(int)(res+0.5));
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章