POJ 1228 Grandpa's Estate (稳定凸包的判定)

Grandpa's Estate
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 11990 Accepted: 3340

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


题意:给你一堆点,这堆点本来就是某个凸包上的部分点,问你这堆点是否能确定唯一的凸包

思路:稳定凸包每条边都要有三个点,所以说在入栈的过程中在同一条直线上的点也要入栈,然后判断是否都是三点
共线就行了。。

ac代码:
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<queue>
#include<map> 
#include<vector>
#define MAXN 1010
#define LL long long
#define INF 0xfffffff
#define mem(x) memset(x,0,sizeof(x))
#define PI acos(-1)
#include<iostream>
#include<algorithm>
using namespace std;
struct s
{
	double x,y;
}list[MAXN];
s stack[MAXN];
int top;
double dis(s aa,s bb)
{
	return sqrt(1.0*((aa.x-bb.x)*(aa.x-bb.x)+(aa.y-bb.y)*(aa.y-bb.y)));
}
double cross(s p0,s p1,s p2)
{
    return (p1.x-p0.x)*(p2.y-p0.y)-(p1.y-p0.y)*(p2.x-p0.x);
}
bool cmp(s aa,s bb)
{
	double num=cross(list[0],aa,bb);
	if(num<0||(!num&&dis(list[0],aa)>dis(list[0],bb))) return false;
	return true;
}
void graham(int n)
{
	int i;
	stack[0]=list[0];
	stack[1]=list[1];
	top=1;
	for(i=2;i<n;i++)
    {
        while(top>=1&&cross(stack[top-1],stack[top],list[i])<0) top--;//共线也入栈
        top++;
        stack[top]=list[i];
    }
}
int main()
{
	int n,i;
	int t;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d",&n);
		for(i=0;i<n;i++)
		scanf("%lf%lf",&list[i].x,&list[i].y);
		if(n<6)
		{
			printf("NO\n");
			continue;
		}
		for(i=1;i<n;i++)
		{
			s temp;
			if(((list[0].y==list[i].y)&&(list[0].x>list[i].x))||list[0].y>list[i].y)
			{
				temp=list[i];
				list[i]=list[0];
				list[0]=temp;
			}
		}
		sort(list+1,list+n,cmp);
		graham(n);
		//stack[top+1]=list[0];
		int bz=0;
		for(i=1;i<top;i++)
		{
			if((cross(stack[i-1],stack[i+1],stack[i]))!=0&&cross(stack[i],stack[i+2],stack[i+1])!=0)//判断三点共线
			{
				bz=1;
				break;
			}
		}
		if(bz==0)
		printf("YES\n");
		else
		printf("NO\n");
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值