poj - 1228 - Grandpa's Estate

来源:互联网 发布:webkaka软件下载 编辑:程序博客网 时间:2024/06/02 15:41

题意:原来一个凸多边形删去一些点后剩n个点,问这个n个点能否确定原来的凸包(1 <= 测试组数t <= 10,1 <= n <= 1000)。

题目链接:http://poj.org/problem?id=1228

——>>初看这题,好别扭,不知道要做什么。。。

其实,是这样的:先求凸包,然后看凸包每一条边所在直线上有多少个点,至少需要3个。

假设一条边的所在直线只有2个点,那么可适当地在这两个点中间加一个或者几个点,使新图形仍是凸包,这时候就不能确定原来的凸包了。

假设一条边的所在直线上有3个以上的点,如果在其中两点间扩展一个点,所形成的图形是凹的,所以不能扩展,即边就确定了。

#include <cstdio>#include <cmath>#include <algorithm>using namespace std;const int maxn = 1000 + 10;const double eps = 1e-10;int dcmp(double x){    if(fabs(x) < eps) return 0;    else return x < 0 ? -1 : 1;}struct Point{    double x;    double y;    Point(double x = 0, double y = 0):x(x), y(y){}    bool operator < (const Point& e) const{        return x < e.x || (dcmp(x - e.x) == 0 && y < e.y);    }}p[maxn], q[maxn];typedef Point Vector;Vector operator + (Point A, Point B){    return Vector(A.x + B.x, A.y + B.y);}Vector operator - (Point A, Point B){    return Vector(A.x - B.x, A.y - B.y);}Vector operator * (Point A, double p){    return Vector(A.x * p, A.y * p);}Vector operator / (Point A, double p){    return Vector(A.x / p, A.y / p);}double Cross(Vector A, Vector B){    return A.x * B.y - B.x * A.y;}int ConvexHull(Point *p, int n, Point* ch){        //求凸包    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-2]) < 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-2]) < 0) m--;        ch[m++] = p[i];    }    if(n > 1) m--;    return m;}double ConvexPolygonArea(Point *p, int n){    double area = 0;    for(int i = 1; i < n-1; i++) area += Cross(p[i]-p[0], p[i+1]-p[0]);    return area / 2;}int main(){    int t, n;    scanf("%d", &t);    while(t--){        bool ok = 1;        scanf("%d", &n);        for(int i = 0; i < n; i++) scanf("%lf%lf", &p[i].x, &p[i].y);        if(n < 3) ok = 0;        else{            int m = ConvexHull(p, n, q);            if(m < 6) ok = 0;            if(ok) for(int i = 2; i < m; i++){                if(dcmp(Cross(q[i] - q[i-1], q[i] - q[i-2])) != 0){                    ok = 0;                    break;                }                while(dcmp(Cross(q[i] - q[i-1], q[i] - q[i-2])) == 0) i++;            }            if(dcmp(ConvexPolygonArea(q, m)) == 0) ok = 0;        }        if(ok) puts("YES");        else puts("NO");    }    return 0;}