Grandpa's Estate(判断是否有点在凸包边上)

题目地址      http://poj.org/problem?id=1228
                                                                                      Grandpa's Estate
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 12582 Accepted: 3531

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

题意:一个由钉子加绳子围成的凸包农场。现在绳子和部分钉子缺失,问能否通过剩余的钉子确定农场有没有变小,没有变小输出YES,由可能变小输出NO。

题解:

由于原本的农场是凸包,所以剩余的点如果能确定农场,那么必定可以围成凸包,且凸包内部没有钉子。

由于要确定农场有没有变小,什么情况下能确定?当围成的凸包每条边上都有至少3个钉子(含端点)。why?因为如果只有两个钉子,那么可能存在一个消失的钉子位于这条边的外面,使得所围的农场变大,且凸包性质没变,这样就不能确定农场是否改变了;若有3个及以上的钉子,那么在边外加钉子的都会把凸包变成凹的,也就不符合农场的定义了。

总的来说,这题就是先用凸包模板找出凸包的端点。然后枚举所有的边,查看是否都有3个以上的钉子即可。


#include <cstdio>
#include <algorithm>
#include <iostream>
using namespace std;
int const MAX = 10005;
struct point{
    int x, y;
    point(int x = 0, int y = 0):x(x), y(y){}
}p[MAX], ch[MAX];
typedef point Vector;  int n;

Vector operator -(Vector const &A, Vector const &B){
    return Vector(A.x - B.x, A.y - B.y);
}
int cross(point a, point b){
    return (a.x * b.y) - (a.y * b.x);
}
bool cmp(point a, point b){
    return a.x < b.x || (a.x == b.x && a.y < b.y);
}

int hull()//这里用Andrew法求凸包,它是Graham—scan算法的变种,和原始的Graham算法相比,Andrew更快,且数值稳定性更好
{
    int m = 0;
    sort(p, p+n, cmp);
    for(int i = 0; i < n; i++){
        while(m > 1 && cross(ch[m-1]-ch[m-2], p[i]-ch[m-2]) <= 0)//这里小于等于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--;//p[0]被重复放入ch中,所以,这里m--之前,m的值应该比ch中的元素个数还要大1,所以应该m--,从而返回ch中的元素个数
    return m;
}

int judge(point a, point b)//判断凸包一条边上是否有三个以上的点
{
    int num = 0;
    for(int i = 0; i < n; i++){
        if(!cross(a-p[i], p[i]-b))
            num++;
    }
    if(num >= 3)   return 1;
    return 0;
}

int main()
{
    int cases;
    scanf("%d", &cases);
    while(cases--){
        scanf("%d", &n);
        for(int i = 0; i < n; i++){
            scanf("%d %d", &p[i].x, &p[i].y);
        }
        if(n < 6){
            printf("NO\n");
            continue;     //这种continue break的善加利用,是应该牢牢记住的
        }
        int top = hull(), f = 0;//这里,hull返回的是顶点数(也就是ch 栈中的元素个数)
        ch[top] = ch[0];
        for(int i = 0; i < top; i++)
             if(!judge(ch[i], ch[i+1])){
                printf("NO\n");
                f = 1;  break;
            }
        if(!f)
            printf("YES\n");
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值