【计算几何/凸包】 POJ 1228 Grandpa's Estate

Grandpa’s Estate

Time Limit: 1000MS Memory Limit: 10000K

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

Tehran 2002 Preliminary

题意

给你一个凸包,判断是否这些凸包可不可能是由其他的凸包删除一些点而得到的,如果有,就输出NO,没有,就说明这个凸包是最大凸包,而不可能是由其他的凸包删除点而来,输出YES。

思路

倘若凸包的部分连线是红线,那么有可能是前一个凸包通过删除绿色的点而得到的。
这里写图片描述
倘若凸包两点之间还有一点在直线上(由于在直线上所以没有加入最后的凸包),那么这两个凸包点之间就是最大凸包的一条边。
因为不可能出现其他删除的点在这凸包之外,若出现了那么就会使这个原来的凸包变成凹陷的多边形,那么就不行了。
即是这种情况
这里写图片描述
倘若有删除掉的点在这条凸包边之外,那么原来的多边形的这条边就会是
这里写图片描述
很明显不OK,题目说了原来的多边形也是凸包。

坑点

我觉得没有,除了题目很难读懂。

AC代码

这里写图片描述

#include<algorithm>
#include<cmath>
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;

class Point///点的定义。
{
    public:
        double x,y;
        Point(double x = 0,double y = 0):x(x),y(y) {}
        Point operator + (Point a)
        {
            return Point(x + a.x, y + a.y);
        }
        Point operator - (Point a)
        {
            return Point(x - a.x, y - a.y);
        }
        bool operator < (const Point &a) const
        {
            if(x==a.x) return y < a.y;
            return x < a.x;
        }
        bool operator == (Point a)
        {
            if(x==a.x && y == a.y) return true;
            return false;
        }
        double abs(void)
        {
            return sqrt(x*x + y*y);
        }
};

typedef vector<Point> Ploygom;
typedef Point Vector;

double cross(Vector a,Vector b)///叉积
{
    return a.x*b.y - a.y*b.x;
}

bool isclockorline(Point p0,Point p1,Point p2)///顺时针方向
{
    Vector a = p1 - p0;
    Vector b = p2 - p0;
    if(cross(a,b) < 0) return true;
    return false;
}

bool online(Point p0,Point p1,Point p2)///判断点在直线上。
{
    if(p1==p0||p1==p2) return true;
    Vector a = p1 - p0;
    Vector b = p2 - p0;
    if(cross(a,b)==0&&a.abs() < b.abs()) return true;
    return false;
}

Ploygom andrewScan(Ploygom p)///构造一个凸包
{
    Ploygom u,l;
    if(p.size() < 3) return p;
    sort(p.begin(),p.end());
    u.push_back(p[0]);
    u.push_back(p[1]);
    l.push_back(p[p.size()-1]);
    l.push_back(p[p.size()-2]);
    for(int i = 2 ; i < p.size() ; i++)
    {
        for(int n = u.size() ; n >= 2 && isclockorline(u[n-2],u[n-1],p[i])!=true ; n--)
            u.pop_back();
        u.push_back(p[i]);
    }
    for(int i = p.size() - 3 ; i >= 0 ; i--)
    {
        for(int n = l.size() ; n >= 2 && isclockorline(l[n-2],l[n-1],p[i])!=true ; n--)
            l.pop_back();
        l.push_back(p[i]);
    }
    for(int i = 1 ; i < l.size() - 1 ; i++)
        u.push_back(l[i]);
    return u;
}

void solve(void)
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        Ploygom a;
        int q;
        scanf("%d",&q);
        while(q--)
        {
            Point t;
            scanf("%lf%lf",&t.x,&t.y);
            a.push_back(t);
        }
        Ploygom n;
        n = andrewScan(a);
        int flag = 0;
        if(a.size()< 6)
        {
            puts("NO");
            goto ee;
        }
        for(int i = 1 ; i < n.size() ; i++)///对于每一对相邻的凸包点来看是否在原来的点集当中存在点在直线上。
        {
            flag = 0;
            for(int j = 0 ; j < a.size() ; j++ )
            {
                Point p = a[j];
                if(online(n[i-1],p,n[i])==true)
                    flag++;
            }
            if(flag<3)
            {
                puts("NO");
                goto ee;
            }
        }
        flag = 0;
        for(int i = 0 ; i < a.size() ; i ++)///别忘了起始点和终点之间的线段。
        {
            Point p = a[i];
            if(online(n[n.size() - 1],p,n[0])==true)
                flag++;
        }
        if(flag < 3){
            puts("NO");
            goto ee;
        }
        puts("YES");
    ee:
        ;
    }
}


int main(void)
{
    solve();
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

两米长弦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值