POJ1228——Grandpa's Estate(计算几何,凸包)

Grandpa's Estate
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 12618 Accepted: 3540

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


给你一个凸包上面的点,在边界上或者在顶点上。询问这是不是一个稳定凸包。

构造一个凸包,判断一下每两个顶点之间是不是还有其他的点与之共线。所有点共线或者只有一个点的时候特判。


#include <stdio.h>
#include <string.h>
#include <vector>
#include <queue>
#include <algorithm>
#include <cmath>
using namespace std;
#define MAXN 1010
#define INF 1000000007
#define EPS 1e-10

//考虑误差的加减运算
double add(double a,double b){
    if(abs(a+b)<EPS*(abs(a)+abs(b)))
        return 0;
    else
        return a+b;
}



struct P{
    double x,y;
    P(){}
    P(double x,double y):x(x),y(y){}
    P operator+(P p){
        return P(add(x,p.x),add(y,p.y));
    };
    P operator-(P p){
        return P(add(x,-p.x),add(y,-p.y));
    };
    P operator *(double d){
        return P(x*d,y*d);
    }
    double dot(P p){ //求内积
        return add(x*p.x,y*p.y);
    }
    double det(P p){ //求外积
        return add(x*p.y,-y*p.x);
    }
};

//字典序
bool cmp_x(const P& p,const P&q){
    if(p.x!=q.x) return p.x<q.x;
    return p.y<q.y;
}

//求凸包
vector <P> convex_hull(P* ps,int n){
    sort(ps,ps+n,cmp_x);
    int k=0; //凸包的顶点数
    vector <P> qs(n*2); //构造中的凸包
    //构造凸包的下侧
    for(int i=0;i<n;i++){
        while(k>1&&(qs[k-1]-qs[k-2]).det(ps[i]-qs[k-1])<0) k--;
        qs[k++]=ps[i];
    }
    //构造凸包的上侧
    for(int i=n-2,t=k;i>=0;i--){
        while(k>t&&(qs[k-1]-qs[k-2]).det(ps[i]-qs[k-1])<0) k--;
        qs[k++]=ps[i];
    }
    qs.resize(k-1);
    return qs;
}


//输入
int n;
P ps[MAXN];

bool solve(){
    vector<P> qs=convex_hull(ps, n);
    int m=(int)qs.size();
    if(m==1)
        return false;
    int ok=0;
    for(int i=0;i<m-2;i++)
    {
        if((qs[i+2]-qs[i+1]).det(qs[i+1]-qs[i])!=0){
            ok=1;
        }
    }
    if(ok==0)
        return false;
    for(int i=1;i<qs.size();i++){
        if((qs[i]-qs[i-1]).det(qs[(i+1)%m]-qs[i])!=0&&((qs[(i+1)%m]-qs[i]).det(qs[(i+2)%m]-qs[(i+1)%m])!=0))
            return false;
    }
    return true;
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(int i=0;i<n;i++)
            scanf("%lf%lf",&ps[i].x,&ps[i].y);
        if(solve())
            printf("YES\n");
        else
            printf("NO\n");
    }
}






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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值