POJ - 1228 Grandpa's Estate (稳定凸包+求凸包模板)

链接:https://cn.vjudge.net/problem/POJ-1228

题意:给出n个点,问这n个点组成的凸包,是否为稳定凸包?

思路:先说一下稳定凸包。稳定凸包就是这个凸包已经是一个极大凸包,不能通过加点再得到一个更大的凸包。怎么判断凸包是否稳定呢?如果一个凸包的的每个边至少有3个点,那么这个凸包是稳定的。这就要求,求凸包的时候把共线点也求出来。

有图链接:https://www.cnblogs.com/xdruid/archive/2012/06/20/2555536.html

 

#include <cstdio>
#include <iostream>
#include <cmath>
#include <algorithm>
#define ll long long
using namespace std;
const int N = 1e4+10;
const double eps = 1e-8;
int sgn(double x)
{
	if(fabs(x)<eps) return 0;
	else if(x<0) return -1;
	else return 1;
}
struct Point
{
	double x,y;
	Point(){}
	Point(double x,double y):x(x),y(y){}
	Point operator -(const Point& b)const//相减 
	{
		return Point(x-b.x,y-b.y);
	}
	double operator ^(const Point& b)const//叉乘 
	{
		return x*b.y-y*b.x;
	}
	double operator *(const Point& b)const//点乘 
	{
		return x*b.x+y*b.y;
	}		
}p[N],q[N],st[N],c[N];
struct Line
{
    Point s,e;
    Line(){}
    Line(Point _s,Point _e)
    {
        s = _s;
        e = _e;
    }
};
int n,pos,top,m;
double x;
double dis(Point a,Point b)
{
	return sqrt((a-b)*(a-b));
}
bool cmp(const Point& a,const Point& b)
{
	x=(a-c[0])^(b-c[0]);
	if(sgn(x)==0)
	{
		return dis(c[0],a)<dis(c[0],b);
	}
	else if(x>0) return 1;
	else return 0;
}
//求凸包时比较 
bool check(Point a,Point b,Point c)
{
	x=(b-a)^(c-a);
	return sgn(x)<0;
}
//将点按逆时针排序 
void sort_point(Point *p,int n)
{
		pos=0;		
		for(int i=0;i<n;i++)
		{
			scanf("%lf%lf",&p[i].x,&p[i].y);
			if(p[i].y<p[pos].y || (p[i].y==p[pos].y&&p[i].x<p[pos].x))
				pos=i;
		}
		swap(p[0],p[pos]);
		for(int i=0;i<n;i++)
			c[i]=p[i];
		sort(c+1,c+n,cmp);
		for(int i=0;i<n;i++)
			p[i]=c[i];	
}
//求凸包 
void getconv(Point *p,int& n)
{
	st[0]=p[0];
	st[1]=p[1];
	top=1;
	for(int i=2;i<n;i++)
	{
		//逆时针必须向左转 
		while(top>1&&check(st[top-1],st[top],p[i])) 
			top--;			
		st[++top]=p[i];
	}
	n=top;		
}
double getarea(Point a,Point b,Point c)
{
	return (c-a)^(b-a);	
}
int main(void)
{
	int t;
	scanf("%d",&t); 
	while(t--)
	{	
		scanf("%d",&n);
		sort_point(p,n);
		if(n<6) 
		{
			puts("NO");
			continue;
		}	
		getconv(p,n);
		bool no=0;
		//稳定凸包,一条边上必须至少有3个顶点 
		for(int i=1;i<n;i++)
		{
			if(sgn(getarea(st[i-1],st[i],st[i+1]))!=0&&sgn(getarea(st[i],st[i+1],st[i+2]))!=0)
			{
				no=1;
				break;
			}
		}
		puts(no?"NO":"YES");
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值