您的位置:首页 > 其它

HDU 1392 (计算几何 凸包)

2015-11-11 11:38 316 查看
题意是给你n个点求凸包的周长。

坑点是当凸包上的点只有两个的时候只要求一条线段的长度。

#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;
typedef unsigned long long ll;
#define maxn 51111
#define pi acos (-1)
#define rotate Rotate

const double eps = 1e-8;
int dcmp (double x) {
    if (fabs (x) < eps)
        return 0;
    else return x < 0 ? -1 : 1;
}
struct point {
    double x, y;
    point (double _x = 0, double _y = 0) : x(_x), y(_y) {}
    point operator - (point a) const {
        return point (x-a.x, y-a.y);
    }
    point operator + (point a) const {
        return point (x+a.x, y+a.y);
    }
    bool operator < (const point &a) const {
        return x < a.x || (x == a.x && y < a.y);
    }
    bool operator == (const point &a) const {
        return dcmp (x-a.x) == 0 && dcmp (y-a.y) == 0;
    }
};

point operator * (point a, double p) {
    return point (a.x*p, a.y*p);
}
double cross (point a, point b) {
    return a.x*b.y-a.y*b.x;
}
double dot (point a, point b) {
    return a.x*b.x + a.y*b.y;
}

int n, m, tot;
point p[maxn], ch[maxn];

int ConvexHull () {
    sort (p, p+n);
    int m = 0;
    for (int i = 0; i < n; i++) {
        while (m > 1 && cross (ch[m-1]-ch[m-2], p[i]-ch[m-1]) <= 0)
            m--;
        ch[m++] = p[i];
    }
    int k = m;
    for (int i = n-2; i >= 0; i--) {
        while (m > k && cross (ch[m-1]-ch[m-2], p[i]-ch[m-1]) <= 0)
            m--;
        ch[m++] = p[i];
    }
    if (n > 1)
        m--;
    return m;
}

double dis (point a, point b) {
    double xx = a.x-b.x, yy = a.y-b.y;
    return sqrt (xx*xx + yy*yy);
}

int main () {
    //freopen ("in", "r", stdin);
    ios::sync_with_stdio(0);
    while (cin >> n && n) {
        for (int i = 0; i < n; i++) {
            cin >> p[i].x >> p[i].y;
        }
        m = ConvexHull (); //if (m == 1 || m== 2) while (1) {}
        if (m == 2) {
            printf ("%.2f\n", dis (ch[0], ch[1]));
            continue;
        }
        double ans = 0;
        for (int i = 0; i < m; i++) {
            ans += dis (ch[i], ch[(i+1)%m]);
        }
        printf ("%.2f\n", ans);
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: