zyf2000

"No regrets."

[POJ1228]Grandpa's Estate(凸包)

题目描述

传送门
题意:给出凸包上的一些点,问是否能唯一确定一个凸包

题解

很显然能确定唯一凸包的条件是凸包上的每一条边上都至少有三个点
求出凸包之后根据叉积判断就行了

  • 至少有三个顶点
  • 每条边上至少有三个点

代码

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
#define N 1005

const double eps=1e-9;
int dcmp(double x)
{
    if (x<=eps&&x>=-eps) return 0;
    return (x>0)?1:-1;
}
struct Vector
{
    double x,y;
    Vector(double X=0,double Y=0)
    {
        x=X,y=Y;
    }
    bool operator < (const Vector &a) const
    {
        return x<a.x||(x==a.x&&y<a.y);
    }
};
typedef Vector Point;
Vector operator - (Vector A,Vector B) {return Vector(A.x-B.x,A.y-B.y);}

int T,n,cnt,top;
double x,y;
Point p[N],stack[N];
int flag[N];
bool ans;

void clear()
{
    n=top=cnt=0;
    x=y=0;
    memset(flag,0,sizeof(flag));
}
double Cross(Vector A,Vector B)
{
    return A.x*B.y-A.y*B.x;
}
void Graham()
{
    sort(p+1,p+n+1);
    for (int i=1;i<=n;++i)
    {
        while (top>1&&dcmp(Cross(stack[top]-stack[top-1],p[i]-stack[top-1]))<0)
            --top;
        stack[++top]=p[i];
    }
    int k=top;
    for (int i=n-1;i>=1;--i)
    {
        while (top>k&&dcmp(Cross(stack[top]-stack[top-1],p[i]-stack[top-1]))<0)
            --top;
        stack[++top]=p[i];
    }
    if (n>1) --top;
}
int main()
{
    scanf("%d",&T);
    while (T--)
    {
        clear();
        scanf("%d",&n);
        for (int i=1;i<=n;++i)
        {
            scanf("%lf%lf",&x,&y);
            p[i]=Point(x,y);
        }
        Graham();
        for (int i=2;i<=top+1;++i)
            if (dcmp(Cross(stack[(i-1)%top+1]-stack[i%top+1],stack[(i+1)%top+1]-stack[i%top+1]))==0)
                ++flag[(i-1)%top+1],++flag[(i+1)%top+1];
            else ++cnt;
        ans=(cnt>=3);
        for (int i=2;i<=top+1;++i)
            if (dcmp(Cross(stack[(i-1)%top+1]-stack[i%top+1],stack[(i+1)%top+1]-stack[i%top+1]))!=0)
                ans&=(flag[i%top+1]>=2);
        if (!ans) puts("NO");
        else puts("YES");
    }
}
阅读更多
版权声明:转载请注明出处:http://blog.csdn.net/clove_unique https://blog.csdn.net/Clove_unique/article/details/53992973
个人分类: 题解 计算几何
相关热词: poj1228
上一篇[POJ2007]Scrambled Polygon(计算几何)
下一篇[POJ1873]The Fortified Forest(dfs+凸包)
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭