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;
    }
    
  • 相关阅读:
    SQLServer数据库自增长标识列的更新修改操作
    “~/Views/Login/Login.aspx”处的视图必须派生自 ViewPage、ViewPage<TModel>、ViewUserControl 或 ViewUserControl<TModel>。
    关于值类型与列类型不匹配,所需类型是 DataRow"的解决方案
    尝试为文件 F:visual studio 2010kbspaperCMSApp_DataProject.mdf 附加自动命名的数据库,但失败。已存在同名的数据库,或指定的文件无法打开或位于 UNC 共享目录中。
    Django快速开发之投票系统
    super() 的入门使用
    [python]模块及包
    [转]大话后端开发的奇淫技巧大集合
    [.net 多线程]ThreadPool的安全机制
    [.net 多线程]Task
  • 原文地址:https://www.cnblogs.com/andyqsmart/p/4101751.html
Copyright © 2011-2022 走看看