zoukankan      html  css  js  c++  java
  • POJ 1113 Wall 求凸包的两种方法

    Wall
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 31199   Accepted: 10521

    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

    题目大意:给定n个点和一个距离L,要求围墙距离任意点的距离大于等于L,求出最小围墙长度。
    方法一:卷包裹法
    思路: 求这n个点的凸包然后再加上以L为半径圆的周长就行了。因为这个总的周长是凸包的长度加上弧的长度,不管弧有多少段,最后一定是对起来一个圆,所以加上一个圆的周长。
    我的代码:
    #include <cstdio>
    #include <iostream>
    #include <algorithm>
    #include <cmath>
    #define EPS 1e-8
    #define PI 3.1415926535897932384626433832795
    using namespace std;
    struct point{
        double x, y;
    };
    const int maxn = 1005;
    point p[maxn];
    int process[maxn];
    int n, l, k;
    bool cmp(const point p1, const point p2)
    {
        return (p1.y == p2.y && p1.x < p2.x || p1.y < p2.y);
    }
    int sgn(double x)
    {
        if (fabs(x) < EPS)
            return 0;
        return x < 0 ? -1 : 1;
    }
    double get_direction(point p1, point p2, point p3)//p1p3在p1p2左侧的时候返回小于0的数 
    {
        return ((p3.x - p1.x) * (p2.y - p1.y) - (p2.x - p1.x) * (p3.y - p1.y));
    }
    double get_distance(point p1, point p2)
    {
        return sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y));
    }
    void jarvis()//卷包裹法求凸包 
    {
        int cur = 0, optimal;//optimal保存最优的点,也就是最外面的点,cur是当前的 
        process[k++] = 0;
        for (int i = 1; i < n; i++)
        {
            for (int j = 1; j < n; j++)
            {
                if (j != cur)
                {
                    optimal = j;
                    break;
                }
            }
            for (int j = 0; j < n; j++)
            {
                if (j != optimal && cur != j && sgn(get_direction(p[cur], p[j], p[optimal])) <= 0)
                {
                    if (sgn(get_direction(p[cur], p[j], p[optimal]) == 0))
                    {
                        if (get_distance(p[cur], p[j]) > get_distance(p[cur], p[optimal]))
                            optimal = j;
                    }
                    else
                        optimal = j;//严格左侧 
                }
            }
            if (optimal == 0)
                break;
            process[k++] = optimal;
            cur = optimal;
        }
    }
    int main()
    {
        while (~scanf("%d %d", &n, &l))
        {
            k = 0;
            for (int i = 0; i < n; i++)
                scanf("%lf %lf", &p[i].x, &p[i].y);
            sort(p, p + n, cmp);
            jarvis();
            double ans = 0;
            for (int i = 1; i <= k; i++)
            {
                ans += get_distance(p[process[i - 1]], p[process[i % n]]);
            }
            ans += 2 * PI * l;
            printf("%d
    ", (int)(ans + 0.5));
        }
        
        
        return 0;
    }
    View Code

     方法二:Graham-Scan方法

    这个方法的主要思想来自极坐标排序,和那个卷包裹法也差不了多少,都是找最外面的那个点,不过这个用的是如果加进来的点右拐的话,就弹出栈顶的点,也就是上一个加进来的点,知道弹到左拐为止,如果左拐直接压栈,这个博客讲的比较好http://www.cnblogs.com/Booble/archive/2011/03/10/1980089.html#2065991,刚开始极角排序的方法理解了,但是平面坐标x,y的一直不理解,因为极角排完序之后一遍for就完事了,很好理解,但是关于平面坐标x, y的方法不太理解,还有就是排序方式也不一样,后来在某篇博客上看到原来一遍只是找到了凸包的一半,虽然是遍历完了,但是,有好多点没有加入,这个它的排序特性所决定的。如果按照y的大小优先排列,那么先找的就是上面的半个凸壳,然后才是下面的凸壳,所以需要两个,这个我是理解了好久了,如果不太理解的话自动动手试试比较好

     用Graham算法来求凸包,平面坐标法
    /*************************************************************************
        > File Name:            poj_1113_graham_coordinate.cpp
        > Author:               Howe_Young
        > Mail:                 1013410795@qq.com
        > Created Time:         2015年04月15日 星期三 21时32分07秒
     ************************************************************************/
    /*平面坐标法求凸包*/ 
    #include <cstdio>
    #include <iostream>
    #include <cstring>
    #include <cmath>
    #include <cstdlib>
    #include <algorithm>
    #include <cstdio>
    #define EPS 1e-8
    #define PI 3.1415926535
    using namespace std;
    typedef long long LL;
    struct point{
        double x, y;
    };
    const int maxn = 1005;
    point p[maxn];
    int n, l, top, convex[maxn];//convex当做栈来使用 
    bool cmp(const point p1, const point p2)//比较函数,不用极角,用坐标(x, y); 
    {
        return ((p1.y == p2.y && p1.x < p2.x) || p1.y < p2.y);
    }
    int sgn(double x)
    {
        if (fabs(x) < EPS)
            return 0;
        return x < 0 ? -1 : 1;
    }
    double get_direction(point p1, point p2, point p3)
    {
        return ((p3.x - p1.x) * (p2.y - p1.y) - (p2.x - p1.x) * (p3.y - p1.y));
    }
    double get_distance(point p1, point p2)
    {
        return sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y));
    }
    void Graham()
    {
        top = 0;
        //找出凸包的下半部分凸壳 
        for (int i = 0; i < n; i++)
        {
            while (top > 1 && sgn(get_direction(p[convex[top - 2]], p[convex[top - 1]], p[i])) > 0)
                top--;
            convex[top++] = i;
        }
        int tmp = top;
        //找出凸包的上半部分,因为我的比较函数是写的y优先的,所以上下部分,当然也可以以x优先排序,这时候就是左右部分了 
        for (int i = n - 2; i >= 0; i--)
        {
            while (top > tmp && sgn(get_direction(p[convex[top - 2]], p[convex[top - 1]], p[i])) > 0)
                top--;
            convex[top++] = i;
        }
    }
    int main()
    {
        while (~scanf("%d %d", &n, &l))
        {
            for (int i = 0; i < n; i++)
            {
                scanf("%lf %lf", &p[i].x, &p[i].y);
            }
            sort(p, p + n, cmp);
            Graham();
            double ans = 0;
            for (int i = 0; i < top - 1; i++)
            {
                ans += get_distance(p[convex[i]], p[convex[i + 1]]);
            }
            ans += 2 * PI * l;
            printf("%d
    ", (int)(ans + 0.5));
        }
        return 0;
    }
    View Code

     Graham算法,极角排序方法

     这个方法是真的按照极角排序来做得,只用一遍的for就行了。只不过排序的时候稍微麻烦点,其他的挺好理解的

    /*极角排序方法*/
    #include <cstdio>
    #include <iostream>
    #include <cstring>
    #include <cmath>
    #include <cstdlib>
    #include <algorithm>
    #include <cstdio>
    #define EPS 1e-8
    #define PI 3.1415926535
    #define INF 99999999
    using namespace std;
    struct point{
        double x, y;
    };
    const int maxn = 1005;
    point p[maxn], pp;//pp保存最小的按个点,就是最左下角的
    int n, l, top, convex[maxn];//convex栈
    int sgn(double x)
    {
        if (fabs(x) < EPS)
            return 0;
        return x < 0 ? -1 : 1;
    }
    double get_direction(point p1, point p2, point p3)
    {
        return ((p3.x - p1.x) * (p2.y - p1.y) - (p2.x - p1.x) * (p3.y - p1.y));
    }
    double get_distance(point p1, point p2)
    {
        return sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y));
    }
    bool cmp(const point p1, const point p2)//极角排序比较函数,以最左下角的点为参考点
    {
        if (sgn(get_direction(pp, p1, p2)) < 0)
            return true;
        if (sgn(get_direction(pp, p1, p2)) == 0 && get_distance(pp, p1) < get_distance(pp, p2))//同样的极角,距离小的排在距离大的前头
            return true;
        return false;        
    }
    void graham()//Graham-Scan算法
    {
        sort(p, p + n, cmp);//先排序
        top = 1;
        if (n == 0)
            return;
        convex[0] = 0;
        if (n == 1)
            return;
        convex[1] = 1;
        for (int i = 2; i < n; i++)//找剩下的n - 2个元素
        {
            while (top > 0 && sgn(get_direction(p[convex[top - 1]], p[convex[top]], p[i])) >= 0)//如果不满足左拐的条件,弹栈
                top--;
            convex[++top] = i;//将这个点压到栈中
        }
    }
    int main()
    {
        while (~scanf("%d %d", &n, &l))
        {
            p.x = pp.y = INF;
            for (int i = 0; i < n; i++)
            {
                scanf("%lf %lf", &p[i].x, &p[i].y);
                if (p[i].x < pp.x || (p[i].x == pp.x && p[i].y < pp.y))
                {
                    pp.x = p[i].x; pp.y = p[i].y;
                }
            }
            graham();
            double ans = 0;
            ++top;
            for (int i = 0; i < top; i++)
            {
                ans += get_distance(p[convex[i]], p[convex[(i + 1)%top]]);
            }
            ans += 2 * PI * l;
            printf("%d
    ", (int)(ans + 0.5));
        }
        return 0;
    }
    View Code
  • 相关阅读:
    主席树模板之区间问题
    简易版第k大(权值线段树+动态开点模板)
    Irrigation
    Petya and Array
    H. Pavel's Party(权值线段树)
    权值线段树入门
    位数差(二分)
    ZYB's Premutation(树状数组+二分)
    单调队列入门
    javaBean为什么要implements Serializable?
  • 原文地址:https://www.cnblogs.com/Howe-Young/p/4427690.html
Copyright © 2011-2022 走看看