【计算几何/凸包】 HDU 1932 Surround the Trees

Surround the Trees

Problem 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.

Output

The minimal length of the rope. The precision should be 10^-2.

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

Source

Asia 1997, Shanghai (Mainland China)

题意

有n棵树,用一根绳子把他们都包起来,求绳子的长度。
凸包裸题。

思路

凸包,计算一下凸包之后的点集两点之间的坐标即可。

坑点

n=2的时候,正常绳子按照凸包算的话长度为 (点1 -> 点2) 的长度 + (点2 -> 点1)的长度。
实际上只用算一边的长度即可,不知道出题人怎么想的。
所以n=2的时候 答案需要除以2。

AC代码

这里写图片描述

#include <bits/stdc++.h>
using namespace std;
#define eps 0.000001
class Point
{
    public:
        double x, y;
        Point(double x = 0, double y = 0):x(x),y(y) {}
        Point operator + (Point a)
        {
            return Point(a.x + x,a.y + y);
        }
        Point operator - (Point a)
        {
            return Point(x - a.x, y - a.y);
        }
        bool operator < (const Point &a) const
        {
            if(x ==a.x)
                return y < a.y;
            return x < a.x;
        }
};

typedef Point Vector;

double cross(Vector a,Vector b)
{
    return a.x*b.y - a.y*b.x;
}

double dot(Vector a,Vector b)
{
    return a.x* b.x + a.y*b.y;
}

bool isclock(Point p0,Point p1,Point p2)
{
    Vector a = p1 - p0;
    Vector b = p2 - p0;
    if(cross(a,b) < -eps) return true;
    return false;
}

double getDistance(Point a,Point b)
{
    return sqrt(pow(a.x-b.x,2)+pow(a.y - b.y,2));
}

typedef vector<Point> Polygon;
Polygon andrewScan(Polygon s)
{
    Polygon u,l;
    if(s.size()<3) return s;
    sort(s.begin(),s.end());
    u.push_back(s[0]);
    u.push_back(s[1]);
    l.push_back(s[s.size() - 1]);
    l.push_back(s[s.size() - 2]);
    for(int i = 2 ; i < s.size() ; i++)
    {
        for(int n = u.size() ; n >= 2 && isclock(u[n-2],u[n-1],s[i])!=true; n--)
            u.pop_back();
        u.push_back(s[i]);
    }
    for(int i = s.size() - 3 ; i >= 0 ; i--)
    {
        for(int n = l.size() ; n >=2 && isclock(l[n-2],l[n-1],s[i])!=true ; n--)
            l.pop_back();
        l.push_back(s[i]);
    }
    //printf("yes\n");
    reverse(l.begin(),l.end());
    //for(auto &p : u) printf("%.2f %.2f\n",p.x,p.y);
    //printf("down\n");
    //for(auto &p : l) printf("%.2f %.2f\n",p.x,p.y);
    for(int i = u.size() - 2; i >= 1 ; i--) l.push_back(u[i]);
    return l;
}

int main(void)
{
    int n;
    while(scanf("%d",&n),n)
    {
        Polygon a;
        for(int i = 0 ; i < n ; i++)
        {
            Point t;
            scanf("%lf%lf",&t.x,&t.y);
            a.push_back(t);
        }
        a = andrewScan(a);
        //printf("yes\n");
        double ans = 0;
        ans += getDistance(a[1], a[0]);
        for(int i = 1 ; i < a.size() - 1 ; i++)
        {
            ans+=getDistance(a[i+1], a[i]);
        }
        ans += getDistance(a[0],a[a.size() - 1]);
        if(n!=2)
            printf("%.2f\n",ans);
        else
            printf("%.2f\n",ans/2);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/peng0614/article/details/81133754