HDU 1392 Surround the Trees (凸包周长)

Posted wulitaotao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU 1392 Surround the Trees (凸包周长)相关的知识,希望对你有一定的参考价值。

题目链接:HDU 1392

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)

Solution

题意:

给定若干个点的坐标,求凸包周长。

思路

凸包

凸包入门模板题,我用的是 \(Andrew\) 算法

Code

#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-8;
const int maxn = 1e5 + 10;

int n;
struct Point 
    double x, y;
    Point() 
    Point(double a, double b) : x(a), y(b) 
    bool operator<(const Point &b) const 
        if (x < b.x) return 1;
        if (x > b.x) return 0;
        return y < b.y;
    
    Point operator-(const Point &b) 
        return Point(x - b.x, y - b.y);
    
 p[maxn], stk[maxn];
typedef Point Vec;

int sgn(double x)  
    if (fabs(x) <= eps)
        return 0;
    return x > 0 ? 1 : -1;


double dist(Point a, Point b) 
    return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));


double cross(Vec a, Vec b) 
    return a.x * b.y - a.y * b.x;


int Andrew() 
    sort(p + 1, p + 1 + n);
    int len = 0;
    for (int i = 1; i <= n; ++i) 
        while (len > 1 && sgn(cross(stk[len] - stk[len - 1], p[i] - stk[len - 1])) == -1) 
            len--;
        
        stk[++len] = p[i];
    
    int k = len;
    for (int i = n - 1; i >= 1; --i) 
        while (len > k && sgn(cross(stk[len] - stk[len - 1], p[i] - stk[len - 1])) == -1) 
            len--;
        
        stk[++len] = p[i];
    
    return len;


int main() 
    while(cin >> n && n) 
        for (int i = 1; i <= n; ++i) 
            scanf("%lf%lf", &p[i].x, &p[i].y);
        
        int t = Andrew();
        double ans = 0;
        for (int i = 1; i < t; i++) 
            ans += dist(stk[i], stk[i + 1]);
        
        printf("%.2lf\n", n == 2 ? ans / 2 : ans);
    
    return 0;

以上是关于HDU 1392 Surround the Trees (凸包周长)的主要内容,如果未能解决你的问题,请参考以下文章

HDU1392 Surround the Trees

HDU - 1392 Surround the Trees (凸包)

[HDU1392]Surround the Trees

HDU ACM 1392 Surround the Trees-&gt;凸包

计算几何初步-凸包-Jarvis步进法。HDU1392Surround the Trees

HDU 1392 凸包子