zoukankan      html  css  js  c++  java
  • ACM学习历程—HDU1392 Surround the Trees(计算几何)

    Description

    There are a lot of trees in an area. A peasant wants to buy a rope to surround all these trees. So at first he must know the minimal required length of the rope. However, he does not know how to calculate it. Can you help him?        The diameter and length of the trees are omitted, which means a tree can be seen as a point. The thickness of the rope is also omitted which means a rope can be seen as a line.      

    There are no more than 100 trees.      

    Input

    The input contains one or more data sets. At first line of each input data set is number of trees in this data set, it is followed by series of coordinates of the trees. Each coordinate is a positive integer pair, and each integer is less than 32767. Each pair is separated by blank.      
    Zero at line for number of trees terminates the input for your program.       
    Sample Input

    9

    12 7

    24 9

    30 5

    41 9

    80 7

    50 87

    22 9

    45 1

    50 7

    0

    Sample Output

    243.06

    这个题目就是求凸包,然后求其凸包的周长。注意判断n为1和n为2的特殊情况。
     
    代码:
    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    #include <set>
    #include <map>
    #include <queue>
    #include <string>
    #include <vector>
    #define INF 0x3fffffff
    
    using namespace std;
    
    struct point
    {
        int x, y;
    };
    
    point p[105], s[105];
    
    bool mult(point sp, point ep, point op)
    {
        return(sp.x - op.x) * (ep.y - op.y) >= (ep.x - op.x) * (sp.y - op.y);
    }
    
    bool operator < (const point &p1, const point &p2)
    {
        return p1.y < p2.y || (p1.y == p2.y && p1.x < p2.x);
    }
    
    int graham(point *p, int n, point *s)
    {
        int len, top = 1;
        sort(p, p + n);
        if (n == 0) return 0;
        s[0] = p[0];
        if (n == 1) return 1;
        s[1] = p[1];
        if (n == 2) return 2;
        s[2] = p[2];
        for (int i = 2; i < n; ++i)
        {
            while (top && mult(p[i], s[top], s[top -1])) top--;
            s[++top] = p[i];
        }
        len = top; s[++top] = p[n-2];
        for (int i = n - 3; i >= 0; --i)
        {
            while (top != len && mult(p[i], s[top], s[top-1])) top--;
            s[++top] = p[i];
        }
        return top;
    }
    
    int main()
    {
        //freopen ("test.txt", "r", stdin);
        int n;
        while (scanf ("%d", &n) != EOF && n != 0)
        {
            for (int i = 0; i < n; ++i)
            {
                scanf ("%d%d", &p[i].x, &p[i].y);
            }
            int len = graham(p, n, s);
            double ans = 0;
            long long temp;
            if (len == 1)
            {
                printf("0.00
    ");
                continue;
            }
            if (len == 2)
            {
                temp = (s[0].x - s[len-1].x) * (s[0].x - s[len-1].x);
                temp += (s[0].y - s[len-1].y) * (s[0].y - s[len-1].y);
                ans += sqrt(temp);
                printf ("%.2lf
    ", ans);
                continue;
            }
            for (int i = 0; i < len; ++i)
            {
                if (i == 0)
                {
                    temp = (s[0].x - s[len-1].x) * (s[0].x - s[len-1].x);
                    temp += (s[0].y - s[len-1].y) * (s[0].y - s[len-1].y);
                    ans += sqrt(temp);
                }
                else
                {
                    temp = (s[i].x - s[i-1].x) * (s[i].x - s[i-1].x);
                    temp += (s[i].y - s[i-1].y) * (s[i].y - s[i-1].y);
                    ans += sqrt(temp);
                }
            }
            printf ("%.2lf
    ", ans);
        }
        return 0;
    }
    
  • 相关阅读:
    TP实例化模型的两种方式 M() D()
    implode 函数 把数组拼接成字符串
    用array_search 数组中查找是否存在这个 值
    SVN-001
    PHP-006
    Access数据操作-02
    Access数据操作-01
    Html解析
    浏览器Chrome对WebGL支持判断
    浏览器渲染模式设置
  • 原文地址:https://www.cnblogs.com/andyqsmart/p/4101751.html
Copyright © 2011-2022 走看看