POJ 1228 Grandpa's Estate(判断是否稳定凸包)

Grandpa's Estate
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 11566 Accepted: 3207

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

给你一些凸包上的点(不含凸包内部点) 判断它是不是一个稳定凸包 。

稳定凸包是指一个凸包 包含原有的点前提下 不能通过加点改变凸包形状

给的点都是凸包顶点 或 凸包边上的点在一条边上只知道两个点时,可以在这条边外加一个点 改变凸包的形状

我们就可以可以在其中一条边上加个点改变其形状


这样的凸包就不是稳定凸包

所以当一条边上最少3个点时,我们是无法在包含原有点的情况下 通过加点改变其形状的


(又盗人家图了闭嘴闭嘴

这样我们只需要判断每条边上是不是至少有三个点即可

#include <iostream>
#include <cstdio>
#include <cstring>
#include <stdlib.h>
#include <algorithm>
#include <queue>
#include <map>
#include <cmath>
#define inf 0x3f3f3f3f
#define PI 3.1415926
#define N 1001
using namespace std;
int m;
struct Point
{
    int x, y;
} pt[N];
int top, tot;
int stack[N];
int mutlti(struct Point pb, struct Point p1, struct Point p2)
{
    return  (p1.x - pb.x) * (p2.y - pb.y) - (p2.x - pb.x) * (p1.y - pb.y);
}
int getdis(Point a, Point b)
{
    return (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y);
}
int dcmp(int x)
{
    if(x == 0) return 0;
    if(x > 0)  return 1;
    return -1;
}
bool cmp( const Point& a, const Point& b)
{
    int d = dcmp( mutlti(pt[0], a, b) ) ;
    if(d == 0) return getdis(pt[0], a) < getdis(pt[0], b);
    return d > 0;
}
int main()
{
    int T;
    scanf("%d", &T);
    while(T --)
    {
        int n, tx, ty,k=0;
        scanf("%d", &n);
        scanf("%d%d", &pt[0].x, &pt[0].y);
        tx = pt[0].x;
        ty = pt[0].y;
        for(int i = 1; i < n; i++)
        {
            scanf("%d%d", &pt[i].x, &pt[i].y);
            int d = dcmp(ty - pt[i].y);
            if(d > 0)
            {
                k = i;
                tx = pt[i].x;
                ty = pt[i].y;
            }
            else if(d == 0 && dcmp(tx - pt[i].x) > 0)
            {
                k = i;
                tx = pt[i].x;
                ty = pt[i].y;
            }
        }
        pt[k].x = pt[0].x;
        pt[k].y = pt[0].y;
        pt[0].x = tx;
        pt[0].y = ty;
        sort(pt + 1, pt + n, cmp);
        stack[0] = 0;
        stack[1] = 1;
        top = 1;
        for( int i = 2; i < n ; i++)
        {
            while( top >= 1 && dcmp( mutlti(pt[stack[top - 1]], pt[i], pt[stack[top]]) ) >= 0 )
            {
                top--;
            }
            stack[++top] = i;
        }
        if( top <= 1)
        {
            printf("NO\n");
            continue;
        }
        bool flag = false;
        for(int i = 1; i <= top; i++)
        {
            if(stack[i] == stack[i-1] + 1)
            {
                flag = true;
                break;
            }
        }
        if(flag)
        {
            printf("NO\n");
            continue;
        }
        for( int i = 1; i < n-1; i++)
        {
            if(dcmp( mutlti(pt[0], pt[i], pt[n-1]) ) == 0)
            {
                flag = true;
                break;
            }
        }
        if(flag)
        {
            printf("YES\n");
        }
        else
        {
            printf("NO\n");
        }
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值