首页 > 代码库 > POJ 1228 - Grandpa's Estate 稳定凸包

POJ 1228 - Grandpa's Estate 稳定凸包

稳定凸包问题 要求每条边上至少有三个点,且对凸包上点数为1,2时要特判

巨坑无比,调了很长时间= =

//POJ 1228
//稳定凸包问题,等价于每条边上至少有三个点,但对m = 1(点)和m = 2(线段)要特判
//AC 2016-10-15

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cmath>
#define MAXN 1010

double sqr(double x){
    return x * x;
}

struct point{
    int x, y;
    point(){}
    point(int X, int Y): x(X), y(Y){}
    friend int operator ^ (const point &p1, const point &p2){
        return p1.x *p2.y - p1.y * p2.x;
    }
    friend int operator * (const point &p1, const point &p2){
        return p1.x *p2.x + p1.y * p2.y;
    }
    double norm(){
        return sqrt(sqr(x) + sqr(y));
    }
    friend point operator >> (const point &p1, const point &p2){
        return point(p2.x - p1.x, p2.y - p1.y);
    }
    friend bool operator < (const point &p1, const point &p2){
        return (p1.x < p2.x)||(p1.x == p2.x)&&(p1.y < p2.y);
    }
}pt[MAXN];

template <typename T>
void swap(T &a, T &b){
    T t = a;
    a = b;
    b = t;
}

template <typename T>
void BBS(T a[], int n){
    for (int i = 0; i < n; i++)
        for (int j = 0; j < i; j++)
            if (a[i] < a[j]) swap(a[i], a[j]);
}

bool stable_convex_hull(point p[], int n){
    int res = 0, cur = 0, m = 0;
    BBS(p, n);
    while(1){
        int tmp = - 1;
        bool stable = 0;
        for (int i = 0; i < n; i++)
            if (i != cur)
                if (!(tmp + 1)){
                    tmp = i, stable = 0;
                }
                else{
                    int det = (p[cur] >> p[i]) ^ (p[cur] >> p[tmp]);
                    if (det > 0){
                        tmp = i, stable = 0;
                    }
                    else if ((!det)&&((p[cur] >> p[i]) * (p[cur] >> p[tmp]) > 0)){
                        if ((p[cur] >> p[i]).norm() > (p[cur] >> p[tmp]).norm())
                            tmp = i;
                        stable = 1;
                    }
                }
        if (tmp + 1){
            m++;
            if (!stable)
                return 0;
        }
        if (!tmp||!(tmp + 1)) return ((tmp + 1) && (m > 2));
        cur = tmp;
    }
}

int main(){
    int t, n;
    freopen("fin.c", "r", stdin);
    scanf("%d", &t);
    while(t--){
        scanf("%d", &n);
        for (int i = 0; i < n; i++){
            scanf("%d%d", &pt[i].x, &pt[i].y);
        }
        if (stable_convex_hull(pt, n)){
            puts("YES");
        }
        else puts("NO");
    }
}

 

POJ 1228 - Grandpa's Estate 稳定凸包