poj 1228 Grandpa's Estate(凸包+判凸包边上的点数)

15 篇文章 0 订阅
3 篇文章 0 订阅

【题目大意】:给出n个凸包上的点(凸包上还有一一些其他的点丢失了),问能否用这些点唯一的确定一个凸包。


【解题思路】:看懂题目大意是第一步。头20分钟一直不知道给定的点是凸包上的点,恶心了很久还是一点思绪都没有。如果给定的点是在凸包上的点,我们不妨通过这些点求一个凸包,如果这个凸包是唯一的必须满足以下的几个条件:

1、所有给定的点都在凸包上。2、每条边上必须至少有3个点。如果一条边上只有一个点的时候,那么我们不妨假设丢失的点在凸包外,那么我们显然构建出了一个新的凸包,反之,如果我们每条边有三个点,那么如果我们在我们求出的凸包外再找一个点,那么势必会使得原先在凸包边上的点变成在凸包内。


另外,这道题有几个小小的tricks...一个是当n<=5的时候是NO,因为最小的凸包必须有3个顶点,为了满足每个边上至少有3个点,则至少要有6个点。另一个是所有点不能在同一条直线上。


【代码】:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <cmath>
#include <string>
#include <cctype>
#include <map>
#include <iomanip>
                   
using namespace std;
                   
#define eps 1e-8
#define pi acos(-1.0)
#define inf 1<<30
#define linf 1LL<<60
#define pb push_back
#define lc(x) (x << 1)
#define rc(x) (x << 1 | 1)
#define lowbit(x) (x & (-x))
#define ll long long

#define INF 999999999.9

struct Point{
    double x, y, dis;
}pt[1005],stack[1005],tmp[1005],p0;//stack为凸包上的点
int top,tot;
int n;
double l;

//计算几何距离
double get_dis(double x1, double y1, double x2, double y2){
    return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}

//极角比较, 返回-1: p0p1在p0p2的右侧,返回0:p0,p1,p2共线
int Cmp_PolarAngel(struct Point p1, struct Point p2, struct Point pb){
    double delta=(p1.x-pb.x)*(p2.y-pb.y)-(p2.x-pb.x)*(p1.y-pb.y);
    if (delta<0.0) return 1;
    else if (delta==0.0) return 0;
    else return -1;
}

// 判断向量p2p3是否对p1p2构成左旋
bool Is_LeftTurn(struct Point p3, struct Point p2, struct Point p1){
    int type=Cmp_PolarAngel(p3, p1, p2);
    if (type<0) return true;
    return false;
}

//先按极角排,再按距离由小到大排
int Cmp(const void*p1, const void*p2){
    struct Point*a1=(struct Point*)p1;
    struct Point*a2=(struct Point*)p2;
    int type=Cmp_PolarAngel(*a1, *a2, p0);
    if (type<0) return -1;
    else if (type==0){
        if (a1->dis<a2->dis) return -1;
        else if (a1->dis==a2->dis) return 0;
        else return 1;
    }
    else return 1;
}

inline int sgn(double x) {
    return (x<-eps)?-1:x>eps;
}

inline bool check(const Point &a, const Point & b) {
    return sgn(a.x-b.x) ==0 && sgn(a.y-b.y)==0;
}

//求凸包
void Solve(int n){
    int k;
    p0.x=p0.y=INF;
    for (int i=0; i<n; i++){
        scanf("%lf %lf",&pt[i].x, &pt[i].y);
        if (pt[i].y < p0.y){
            p0.y=pt[i].y;
            p0.x=pt[i].x;
            k=i;
        }
        else if (pt[i].y==p0.y){
            if (pt[i].x<p0.x){
                p0.x=pt[i].x;
                k=i;
            }
        }
    }
    if (n<=5) {cout << "NO" <<endl; return ;}
    pt[k]=pt[0];
    pt[0]=p0;
    for (int i=1; i<n; i++)
        pt[i].dis=get_dis(pt[i].x,pt[i].y,p0.x,p0.y);
    qsort(pt+1, n-1, sizeof(struct Point), Cmp);
    for (int i=0; i<n; i++) tmp[i]=pt[i];
    //去掉极角相同的点
    tot=1;
    for (int i=2; i<n; i++)
        if (Cmp_PolarAngel(pt[i], pt[i-1], p0))
            pt[tot++]=pt[i-1];
    pt[tot++]=pt[n-1];
    //求凸包
    top=1;
    stack[0]=pt[0];
    stack[1]=pt[1];
    for (int i=2; i<tot; i++){
        while (top>=1 && Is_LeftTurn(pt[i],stack[top],stack[top-1])==false){ 
            top--;
        }
        stack[++top]=pt[i];
    } 
    int cnt,cnt1=0;
    bool flag=true;
    stack[top+1]=stack[0];
    for (int i=0; i<=top; i++){
        cnt=0;
        for (int j=0; j<n; j++){
            if (check(stack[i],tmp[j]) || check(stack[i+1],tmp[j])) continue;
            else{
                if (Cmp_PolarAngel(stack[i],stack[i+1],tmp[j])==0) { 
                    cnt++;
                }
            }
        }
        if (cnt==0 || cnt+2==n) {flag=false; break;}
        else cnt1+=cnt;
    }
    if (cnt1+top+1==n) if (flag) cout << "YES" << endl;
                        else cout << "NO" << endl;
    else cout << "NO" << endl; 
    return ;
}

int main(){
    int T;
    cin >> T;
    while (T--){
        scanf("%d",&n);
        Solve(n);
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值