POJ 1228 (计算几何 凸包)

题意是给你n个点,问它们构成的凸包能不能扩大了。

板子题,暴力枚举凸包每条边,只要每条边上都有点(不包含端点),那么这个凸包就不能扩大,否则肯定能扩大。特判1个点和2个点的凸包。

#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;
typedef unsigned long long ll;
#define maxn 1111
#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;
}

bool PointOnSegment (point p, point a1, point a2) { //点在线段上(不包含端点)
    return dcmp (cross (a1-p, a2-p)) == 0 && dcmp (dot (a1-p, a2-p)) < 0;
}

void solve () {
    if (m == 1 || m == 2) {
        printf ("NO\n");
        return ;
    }
    bool vis[maxn];
    memset (vis, 0, sizeof vis);
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++) {
            if (PointOnSegment (p[j], ch[i], ch[(i+1)%m]))
                vis[i] = 1;
        }
    }
    for (int i = 0; i < m; i++) {
        if (!vis[i]) {
            printf ("NO\n");
            return ;
        }
    }
    printf ("YES\n");
    return ;
}

int main () {
    //freopen ("in", "r", stdin);
    int t;
    cin >> t;
    while (t--) {
        cin >> n;
        for (int i = 0; i < n; i++) {
            cin >> p[i].x >> p[i].y;
        }
        m = ConvexHull ();
        solve ();
    }
    return 0;
}


你可能感兴趣的:(POJ 1228 (计算几何 凸包))