1. 程式人生 > 實用技巧 >POJ - 1113 Wall (凸包模板) Graham Scan 演算法實現

POJ - 1113 Wall (凸包模板) Graham Scan 演算法實現

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.

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

結果四捨五入就可以了

Source

Northeastern Europe 2001

簡化下題意即求凸包的周長+2×PI×r。

這道題的答案是凸包周長加上一個圓周長,即包圍凸包的一個圓角多邊形,但是沒弄明白那些圓角加起來為什麼恰好是一個圓。每個圓角是以凸包對應的頂點為圓心,給定的L為半徑,與相鄰兩條邊的切點之間的一段圓弧。每個圓弧的兩條半徑夾角與對應的凸包的內角互補。假設凸包有n條邊,則所有圓弧角之和為180°n-180°(n-2)=360°。故,圍牆周長為=n條平行於凸包的線段+n條圓弧的長度=凸包周長+圍牆離城堡距離L為半徑的圓周長。

AC程式碼:學習自KB大佬的模板加以修改

// Author : RioTian
// Time : 20/10/21
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;
const int N = 1000;
const double Pi = acos(-1.0);

struct point {
    int x, y;
    point() : x(), y() {}
    point(int x, int y) : x(x), y(y) {}
} list[N];
typedef point P;
int stack[N], top;
//計算叉積: p0p1 X p0p2
int cross(P p0, P p1, P p2) {
    return (p1.x - p0.x) * (p2.y - p0.y) - (p2.x - p0.x) * (p1.y - p0.y);
}
//計算 p1p2的 距離
double dis(P p1, P p2) {
    return sqrt((double)(p2.x - p1.x) * (p2.x - p1.x) +
                (p2.y - p1.y) * (p2.y - p1.y));
}
//利用極角排序,角度相同則距離小排前面
bool cmp(P p1, P p2) {
    int tmp = cross(list[0], p1, p2);
    if (tmp > 0)
        return true;
    else if (tmp == 0 && dis(list[0], p1) < dis(list[0], p2))
        return true;
    else
        return false;
}
//輸入,並把  最左下方的點放在 list[0]  。並且進行極角排序
void init(int n) {
    int i, k = 0;
    cin >> list[0].x >> list[0].y;
    P p0 = list[0];  // p0 等價於 tmp 去尋找最左下方的點
    for (int i = 1; i < n; ++i) {
        cin >> list[i].x >> list[i].y;
        if (p0.y > list[i].y || (p0.y == list[i].y && p0.x > list[i].x))
            p0 = list[i], k = i;
    }
    list[k] = list[0];
    list[0] = p0;
    sort(list + 1, list + n, cmp);
}
//graham掃描法求凸包,凸包頂點存在stack棧中
//從棧底到棧頂一次是逆時針方向排列的
//如果要求凸包的一條邊有2個以上的點
//那麼要將while中的<=改成<
//但這不能將最後一條邊上的多個點保留
//因為排序時將距離近的點排在前面
//那麼最後一條邊上的點僅有距離最遠的會被保留,其餘的會被出棧
//所以最後一條邊需要特判
//如果要求逆凸包的話需要改cmp,graham中的符號即可
void Graham(int n) {
    int i;
    if (n == 1) top = 0, stack[0] = 0;
    if (n == 2) top = 1, stack[0] = 0, stack[1] = 1;
    if (n > 2) {
        for (i = 0; i <= 1; i++) stack[i] = i;
        top = 1;

        for (i = 2; i < n; i++) {
            while (top > 0 &&
                   cross(list[stack[top - 1]], list[stack[top]], list[i]) <= 0)
                top--;
            top++;
            stack[top] = i;
        }
    }
}
int main() {
    // freopen("in.txt", "r", stdin);
    // ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    int N, L;
    while (scanf("%d%d", &N, &L) != EOF) {
        init(N);
        Graham(N);
        //叉積求凸包面積
        double res = 0;
        for (int i = 0; i < top; i++)
            res += dis(list[stack[i]], list[stack[i + 1]]);
        res += dis(list[stack[0]], list[stack[top]]);

        res += 2 * Pi * L;
        printf("%d\n", (int)(res + 0.5));
    }
}