POJ 1228 Grandpa's Estate (凸包唯一性判定 推荐)

77 篇文章 8 订阅
22 篇文章 0 订阅
Grandpa's Estate
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 12855 Accepted: 3618

Description

Being the only living descendant of his grandfather, Kamran the Believer inherited all of the grandpa's belongings. The most valuable one was a piece of convex polygon shaped farm in the grandpa's birth village. The farm was originally separated from the neighboring farms by a thick rope hooked to some spikes (big nails) placed on the boundary of the polygon. But, when Kamran went to visit his farm, he noticed that the rope and some spikes are missing. Your task is to write a program to help Kamran decide whether the boundary of his farm can be exactly determined only by the remaining spikes.

Input

The first line of the input file contains a single integer t (1 <= t <= 10), the number of test cases, followed by the input data for each test case. The first line of each test case contains an integer n (1 <= n <= 1000) which is the number of remaining spikes. Next, there are n lines, one line per spike, each containing a pair of integers which are x and y coordinates of the spike.

Output

There should be one output line per test case containing YES or NO depending on whether the boundary of the farm can be uniquely determined from the input.

Sample Input

1
6 
0 0
1 2
3 4
2 0
2 4 
5 0

Sample Output

NO

Source

Tehran 2002 Preliminary

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

题目大意:给出一个凸包的一部分,问给定的部分能否确定唯一的一个凸包,也就是说加额外的点不能变成另一个凸包

题目分析:这题真是怎样都能过,数据太水了,我的思路是先用Graham扫描求凸包,然后枚举凸包的边,再枚举所有点,如果某条边上只有两个端点,则凸包不唯一,复杂度是n^2的,由于凸包的一条边有至少三个点时这条边才唯一确定, 故点数最少的唯一情况是一个三角形的三边每边多出一个点,总共是6个点,所以可以加个特判。AC后看了网上的一些做法,感觉不太正确的样子,基本上是这样:改模板求出凸包(包含共线点)然后枚举点判断是否存在只有两个端点的边,其实这样做感觉没必要求凸包了,因为题目给的本身就是凸包的点,只需要取左下的点极角排序一下,但是极角排序在共线的情况下是短边在前,因此On仿佛没法直接判断,
比如这样的情况
8                  极角排序完是      8            (3, 2)后的下一个点是(0, 1)而不是(0, 3),所以感觉这样做是不是不对?
0  0                                          0  0
0  1                                          1  1
0  2                                          2  2
0  3                                          3  3
1  1                                          3  2
2  2                                          0  1
3  3                                          0  2
2  3                                          0  3

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
int const MAX = 1005;
int n;

struct POINT {
    int x, y;
}p[MAX], stk[MAX], base;
int top;

double getDist(POINT a, POINT b) {
    return sqrt(1.0 * (a.x - b.x) * (a.x - b.x) + 1.0 * (a.y - b.y) * (a.y - b.y));
}

int getCross(POINT p0, POINT p1, POINT p2) {
    return (p1.x - p0.x) * (p2.y - p0.y) - (p2.x - p0.x) * (p1.y - p0.y);
}

bool cmp(POINT a, POINT b) {
    if (getCross(base, a, b) == 0) {
        return getDist(base, a) < getDist(base, b);
    }
    if (getCross(base, a, b) > 0) {
        return true;
    }
    return false;
}

void getBase() {
    scanf("%d %d", &p[0].x, &p[0].y);
    base.x = p[0].x;
    base.y = p[0].y;
    int pos = 0;
    for (int i = 1; i < n; i ++) {
        scanf("%d %d", &p[i].x, &p[i].y);
        if (p[i].y < base.y || (p[i].y == base.y && p[i].x <= base.x)) {
            base.x = p[i].x;
            base.y = p[i].y;
            pos = i;
        }
    }
    swap(p[0], p[pos]);
}

bool equals(POINT a, POINT b){
    return (a.x == b.x && a.y == b.y);
}

bool onSeg(POINT p0, POINT p1, POINT q) {
    //q在p0p1线段所在直线上
    if (getCross(p0, p1, q) == 0) {
        //q在p0p1线段上且不包括端点
        if ((p0.x - q.x) * (p1.x - q.x) <= 0 && (p0.y - q.y) * (p1.y - q.y) <= 0) {
            return true;
        }
    }
    return false;
}

bool judge() {
    for (int i = 0; i < top; i ++) {
        bool flag = false;
        for (int j = 0; j < n; j ++) {
            //枚举点不与端点重合
            if (!equals(stk[i], p[j]) && !equals(stk[i + 1], p[j])) {
                if (onSeg(stk[i], stk[i + 1], p[j])) {
                    flag = true;
                    break;
                }
            }
        }
        if (!flag) {
            return false;
        }
    }
    return true;
}

int main() {
    int T;
    scanf("%d", &T);
    while (T --) {
        scanf("%d", &n);
        getBase();
        if (n < 6) {
            printf("NO\n");
            continue;
        }
        sort(p + 1, p + n, cmp);
        stk[0] = p[0];
        stk[1] = p[1];
        top = 1;
        for (int i = 2; i < n; i ++) {
            while (top > 0 && getCross(stk[top - 1], stk[top], p[i]) <= 0) {
                top --;
            }
            stk[++ top] = p[i];
        }  
        //已知凸包中只有一个点或者两个点显然是不唯一的
        if (top < 2) {
            printf("NO\n");
            continue;
        }
        stk[++ top] = p[0];
        printf("%s\n", judge() ? "YES" : "NO");
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值