码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 1392 Surround the Trees

时间:2014-07-22 23:03:14      阅读:285      评论:0      收藏:0      [点我收藏+]

标签:blog   os   io   2014   for   re   

PS: 在求解两个点的时候就是两个点的距离,在这WA了一次。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std;
const int maxn = 110;

struct point {
    int x, y;
    point(double x=0, double y=0):x(x),y(y) {}
};
point operator - (point A, point B) {
    return point(A.x-B.x, A.y-B.y);
}

double Dist(point A, point B) {
    double x = (A.x-B.x)*(A.x-B.x);
    double y = (A.y-B.y)*(A.y-B.y);
    return sqrt(x+y);
}
point ch[maxn];
vector<point> v;
int n;

double Cross(point A, point B) {
    return A.x*B.y - A.y*B.x;
}

bool cmp(point a, point b) {
    if(a.x!=b.x) return a.x < b.x;
    else return a.y<b.y;
}
int ConvexHull() {
    int m = 0;
    for(int i = 0; i < n; i++) {
        while(m>1 && Cross(ch[m-1]-ch[m-2], v[i]-ch[m-2])<=0)
            m--;
        ch[m++] = v[i];
    }
    int k = m;
    for(int i = n-2; i >= 0; i--) {
        while(m>k && Cross(ch[m-1]-ch[m-2], v[i]-ch[m-2])<=0)
            m--;
        ch[m++] = v[i];
    }
    if(n>1) m--;
    return m;
}

int main()
{
    point t;
    while(scanf("%d", &n) && n) {
        v.clear();
        for(int i = 0; i < n; i++) {
            scanf("%d%d", &t.x, &t.y);
            v.push_back(t);
        }
        sort(v.begin(), v.end(), cmp);
        int m = ConvexHull();
        double res = 0;
        if(m==2) {
            res = Dist(ch[0], ch[1]);
            printf("%.2lf\n", res);  // res*2 WA.
            continue;
        }
        for(int i = 0; i < m-1; i++) {
            res += Dist(ch[i], ch[i+1]);
        }
        res += Dist(ch[m-1], ch[0]);
        printf("%.2lf\n", res);
    }
    return 0;
}

HDU 1392 Surround the Trees,码迷,mamicode.com

HDU 1392 Surround the Trees

标签:blog   os   io   2014   for   re   

原文地址:http://blog.csdn.net/achiberx/article/details/24738879

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!