[POJ1228]Grandpa's Estate(凸包)

题目描述

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

题解

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

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

代码

#include
#include
#include
#include
#include
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 xtypedef 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");
    }
}

你可能感兴趣的:(题解,计算几何)