poj 1228 Grandpa's Estate

题意:判断给出的点组成的凸包是不是稳定凸包

稳定土包:凸包的每条边上至少有三个点

题意略难理解..............



#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const int MAXN = 10005;
struct node
{
    double x,y;
}p[MAXN];
int res[MAXN],top,n;
bool vis[MAXN];

bool cmp(const node &a, const node &b)
{
    if(a.y == b.y)
        return a.x < b.x;
    return a.y < b.y;
}

double multi(node &a, node &b, node &c)
{
    return (a.x - c.x)*(b.y - c.y) - (b.x - c.x)*(a.y - c.y);
}

void Graham()
{
    int len;
    top = 1;

    sort(p,p+n,cmp);
    for(int i = 0; i < 3; i++)
        res[i] = i;
    for (int i = 2; i < n; ++i)
    {
        while(top && multi(p[i], p[res[top]], p[res[top-1]]) >= 0)
            top--;
        res[++top] = i;
    }
    len = top;
    res[++top] = n-2;
    for(int i = n-3; i >= 0; i--)
    {
        while(top != len && multi(p[i], p[res[top]], p[res[top-1]]) >= 0)//res只保留了凸包的各个顶点
            top--;
        res[++top] = i;
    }
}

int main(int argc, char const *argv[])
{
    int t,flag;

    scanf("%d", &t);
    while(t--)
    {
        scanf("%d", &n);
        for(int i = 0; i < n; i++)
            scanf("%lf %lf", &p[i].x, &p[i].y);

        if(n < 6)//n=6时,3个点为顶点,3个点在边上
            printf("NO\n");
        else
        {
            Graham();
            for(int i = 0; i <= n; i++)
                vis[i] = false;//初始化
            for(int i = 0; i < top; i++)
                vis[res[i]] = true;//不是土包顶点的vis值为false

            for(int i = 0; i < top; i++)//遍历所有的边
            {
                flag = 0;
                for(int j = 0; j < n; j++)//遍历所有点,是否有点是在边上的,若没有就输出NO
                {
                    if(!vis[j])
                    {
                        if(multi(p[res[i+1]], p[j], p[res[i]]) == 0)
                        {
                            flag = 1;
                            vis[j] = true;
                        }
                    }
                }//找过所有的点,判断
                if(!flag)
                {
                    printf("NO\n");
                    break;
                }
            }
            if(flag)//所有的边除了端点,还有点
                printf("YES\n");
        }
    }

    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值