HDUOJ 1392 - Surrounding the Trees

54 篇文章 0 订阅
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
 


是一道凸包的题,给出好多树的坐标,计算出能够围起所有树的最小长度。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
using namespace std;

struct node
{
    int x, y;
};
node point[1005];
node stk[1005];

double Distance(node a, node b)
{
    return sqrt((a.x - b.x)*(a.x - b.x) + (a.y - b.y)*(a.y - b.y));
}

int Multiply(node a, node b, node c)
{
    int abx = b.x - a.x;
    int aby = b.y - a.y;
    int acx = c.x - a.x;
    int acy = c.y - a.y;
    return abx*acy - acx*aby;
}

bool cmp_normal(node a, node b)
{
    if (a.y == b.y)
        return a.x < b.x;
    return a.y < b.y;
}

bool cmp_Multiply(node a, node b)///按照叉积排序
{
    int sym = Multiply(point[0], a, b);
    if (sym == 0)
        return Distance(point[0], a) - Distance(point[0], b) <= 0 ? true : false;
    return sym > 0 ? true : false;
}

int main()
{
    int n;
    double length;
    while (scanf("%d", &n) != EOF && n != 0)
    {
        if (n == 1)
        {
            printf("0.00\n");
            continue;
        }
        for (int i = 0; i < n; ++i)
            scanf("%d%d", &point[i].x, &point[i].y);
        if (n == 2)
        {
            printf("%.2f\n", Distance(point[0], point[1]));
            continue;
        }
        sort(point, point + n, cmp_normal);///找出最下面的
        sort(point + 1, point + n, cmp_Multiply);///按照凸包的规律排序
        memset(stk, 0, sizeof(stk));
        stk[0] = point[0];
        stk[1] = point[1];
        int top = 1;

        for (int i = 2; i < n; ++i)
        {
            while (i >= 1 && Multiply(stk[top-1], stk[top], point[i]) < 0)
                top--;
            stk[++top] = point[i];
        }
        length = 0;
        for (int i = 1; i <= top; ++i)
            length += Distance(stk[i-1], stk[i]);
        length += Distance(stk[top], point[0]);
        printf("%.2f\n", length);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值