Grandpa's Estate POJ - 1228 (稳定凸包)

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 <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#include <map>
#include <vector>
#include <set>
#include <string>
#include <math.h>
#include <stack>
#define Lson l,m,rt<<1
#define Rson m+1,r,rt<<1|1
typedef long long ll;
#define INF 0x3f3f3f3f
const int maxn=2e4+5;
const int MAXN=1e3+10;
const long long mod=100000000;
using namespace std;
const double eps=1e-8;
const double PI=acos(-1.0);
struct point
{
	int x,y;
}p[maxn];
int n;
int sta[30],top;
 
int cross(point p0,point p1,point p2)//p0p1 * p0p2叉积  判断顺/逆时针 
{
	return (p1.x-p0.x)*(p2.y-p0.y)-(p1.y-p0.y)*(p2.x-p0.x);
}
double dis(point p1,point p2)
{
	return sqrt((double)(p2.x-p1.x)*(p2.x-p1.x)+(p2.y-p1.y)*(p2.y-p1.y));
}
bool cmp(point p1,point p2)//极角排序 p[0]为最下方&&最左边的点 
{
	int tmp=cross(p[0],p1,p2);
	if(tmp>0) return true;
	else if(tmp==0&&dis(p[0],p1)<dis(p[0],p2)) return true;//角度相同,距离小在前
	else return false;
}
void Graham_scan(int n)
{
    if(n==1)
    {
        sta[0]=0;
        top=0;
        return;
    }
    int index=0;
    for(int i=1;i<n;i++)
    {
        if(p[i].y<p[index].y||(p[i].y==p[index].y&&p[i].x<p[index].x))
        {
            index=i;
        }
    }
    swap(p[0],p[index]);
    sort(p+1,p+n,cmp);
    if(n==2)
    {
        sta[0]=0,sta[1]=1,top=1;
    }
    else if(n>2)
    {
        for(int i=0;i<=1;i++) sta[i]=i;
        top=1;
        for(int i=2;i<n;i++)
        {
            while(top>0&&cross(p[sta[top-1]],p[sta[top]],p[i])<0)
            {
                top--;
            }
            sta[++top]=i;
        }
    }
}
int main(int argc, char const *argv[])
{
    #ifndef ONLINE_JUDGE
        freopen("in.txt","r",stdin);
        freopen("out.txt","w",stdout);
    #endif
    int T;
    cin>>T;
    while(T--)
    {
        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;
        }
        Graham_scan(n);
        bool flag=1;
        for(int i=1;i<top;i++)
        {
            if(cross(p[sta[i-1]],p[sta[i+1]],p[sta[i]])!=0&&cross(p[sta[i]],p[sta[i+1]],p[sta[(i+2)%(top+1)]])!=0)
            {
                flag=0;
                break;
            }
        }
        if(flag==1)
        {
            printf("YES\n");
        }
        else
        {
            printf("NO\n");
        }
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值