POJ 1228 Grandpa's Estate 【计算几何:凸包,andrew】

Grandpa's Estate
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 9331 Accepted: 2489

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


题目大意:

给出若干点,问这些点是否可以确定一个凸包。
发现实际上只要凸包上每条边上都至少有一个点(不包括端点)就可以了。
因为如果某条边上没有另一个点的话,可以将这条边想象成橡皮筋,把这个皮筋拉到外面,但这样还是可以构成一个凸包。

一般求凸包时是用graham裹包算法,但这道题求出来的凸包要求是不包含边上的点的。虽然可以通过把修改判断退栈的条件(加上等于)来实现。但在最后一边的时候会出现问题,最后一条边还是会有同一条边上点被加进来。我想到的方法是将第一遍求出来的凸包的那些点,按照负的极脚再弄一遍。但这样显然很麻烦!
所以学习了另一个简单实用的算法,Andrew。是graham算法的改进,具体流程是将点按先x后y排序,以最左方点开始,先求下凸包,然后再求上凸包。代码实现就是两遍的扫瞄。具体可以看白书

代码:

#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
const double eps = 1e-8;
struct point{
	double x,y;
	friend bool operator < (const point &a,const point &b){
		if(a.x==b.x)return a.y<b.y;
		else return a.x<b.x;
	}
};
point p[1001];
int top,st[1001];
double cross(const point &o,const point &a,const point &b){ return (a.x-o.x)*(b.y-o.y)-(a.y-o.y)*(b.x-o.x); }
double get_dis(const point &a,const point &b){
	return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
bool dy(double a,double b){return a>b+eps;}
bool xy(double a,double b){return a<b-eps;}
bool dyd(double a,double b){return a>b-eps;}
bool xyd(double a,double b){return a<b+eps;}
bool eq(double a,double b){return fabs(a-b)<eps;}
bool trangle_cmp(const point &a,const point &b){
	if(fabs(cross(p[0],a,b))<eps){
		return get_dis(p[0],a)<get_dis(p[0],b);
	}else return cross(p[0],a,b)>0;
}
bool andrew(int n){
	sort(p,p+n);top=0;
	for(int i=0;i<n;i++){
		while(top>1&&dyd(cross(p[st[top-1]],p[st[top-2]],p[i]),0))top--; st[top++]=i;
	}
	int k=top;
	for(int i=n-2;i>=0;i--){
		while(top>k&&dyd(cross(p[st[top-1]],p[st[top-2]],p[i]),0))top--;st[top++]=i;
	}
	if(n>1)top--;
	st[top]=st[0];
	for(int i=0;i<top;i++){
		bool find=false;
		for(int j=0;j<n;j++){
			if(j==st[i]||j==st[i+1])continue;
			if(eq(cross(p[st[i]],p[st[i+1]],p[j]),0))find=true;
		}
		if(!find)return false;
	}
	if(top>2)return true;
	return false;
}
int main(){
	int n;
	int T;
	scanf("%d",&T);
	while(T--){
		scanf("%d",&n);
		for(int i=0;i<n;i++){
			scanf("%lf%lf",&p[i].x,&p[i].y);
		}
		if(andrew(n))printf("YES\n");
		else printf("NO\n");
	}
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值