POJ1228-Grandpa's Estate(凸包)

Grandpa‘s Estate

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 10851   Accepted: 2953

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

题意:凸包上有n个点,问你这个凸包是否唯一确定。

思路:如果边上没点,那么就不能确定。两点之间可以有个凸起的点,如果边上有点,就保证了原来中间不可能有凸起的点,不然边上的点就不可能存在。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;
#define REP(_,a,b) for(int _ = (a); _ < (b); _++)
#define sz(s)  (int)((s).size())
typedef long long ll;
const double eps = 1e-10;
const int maxn = 1000+10;
int n;
struct Point{
	double x,y;
	Point(double x=0.0,double y = 0.0):x(x),y(y){}
};
Point vP[maxn];
Point poly[maxn];
typedef Point Vector;
Vector operator + (Vector A,Vector B) {
	return Vector(A.x+B.x,A.y+B.y);
}
Vector operator - (Vector A,Vector B){
	return Vector(A.x-B.x,A.y-B.y);
}
Vector operator * (Vector A,double p){
	return Vector(A.x*p,A.y*p);
}
Vector operator / (Vector A,double p){
	return Vector(A.x/p,A.y/p);
}
int dcmp(double x){
	if(fabs(x) < eps) return 0;
	else return x < 0? -1:1;
}
bool operator < (const Point &a,const Point &b){
	return dcmp(a.x-b.x) <0 || dcmp(a.x-b.x)==0&&dcmp(a.y-b.y)<0;
}

bool operator == (const Point &a,const Point &b){
	return dcmp(a.x-b.x)==0&& dcmp(a.y-b.y)==0;
}
double Dot(Vector A,Vector B) {return A.x*B.x+A.y*B.y;}
double Length(Vector A) {return sqrt(Dot(A,A));}
double Angle(Vector A,Vector B) {return acos(Dot(A,B)/Length(A)/Length(B));}
double Cross(Vector A,Vector B) {return A.x*B.y-A.y*B.x;}
Vector Rotate(Vector A,double rad) {return Vector(A.x*cos(rad)-A.y*sin(rad),A.x*sin(rad)+A.y*cos(rad)); }

Vector Normal(Vector A) {
	double L = Length(A);
	return Vector(-A.y/L,A.x/L);
}

bool OnSegment(Point p,Point a1,Point a2){
	return dcmp(Cross(a1-p,a2-p)) == 0 && dcmp(Dot(a1-p,a2-p)) < 0;
}

int ConvexHull(Point* p,int n,Point *ch){
	sort(p,p+n);
	int m = 0;
	for(int i = 0; i < n; i++) {
		while(m > 1 && dcmp(Cross(ch[m-1]-ch[m-2],p[i]-ch[m-2])) <= 0) m--;
		ch[m++] = p[i];
	}
	int k = m;
	for(int i = n-2; i >= 0; i--){
		while(m > k && dcmp(Cross(ch[m-1]-ch[m-2],p[i]-ch[m-2])) <= 0) m--;
		ch[m++] = p[i];
	}
	if(n > 1) m--;
	return m;
}

void input(){
	scanf("%d",&n);
	REP(i,0,n){
		scanf("%lf%lf",&vP[i].x,&vP[i].y);
	}
}
void solve(){
	int m = ConvexHull(vP,n,poly);
	if(n < 6 || m < 3){
		printf("NO\n");
		return;
	}
	bool flag = true;
	REP(i,1,m) {
		int cnt = 0;
		REP(j,0,n) {
			if(OnSegment(vP[j],poly[i],poly[i-1])){
				cnt++;
			}
		}
		if(cnt==0){
			flag = false;
			break;
		}
	}
	if(flag){
		printf("YES\n");
	}else{
		printf("NO\n");
	}
}
int main(){

	int ncase;
	cin >> ncase;
	while(ncase--){
		input();
		solve();
	}
	return 0;
}

POJ1228-Grandpa's Estate(凸包),布布扣,bubuko.com

POJ1228-Grandpa's Estate(凸包)

时间: 2024-10-09 14:09:11

POJ1228-Grandpa's Estate(凸包)的相关文章

poj1228 Grandpa&#39;s Estate 凸包

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 se

POJ1228 Grandpa&#39;s Estate 稳定凸包

POJ1228 转自http://www.cnblogs.com/xdruid/archive/2012/06/20/2555536.html   这道题算是很好的一道凸包的题吧,做完后会加深对凸包的理解.    题意很关键...这英语看了好几遍才差不多看明白了.意思就是给你一堆点,这堆点本来就是某个凸包上的部分点,问你这堆点是否能确定唯一的凸包(大概这意思吧...).后来搜了一下,发现这种凸包叫做稳定凸包. 首先来了解什么是稳定的凸包.比如有4个点: 这四个点是某个凸包上的部分点,他们连起来后

【POJ 1228】Grandpa&#39;s Estate 凸包

找到凸包后暴力枚举边进行$check$,注意凸包是一条线(或者说两条线)的情况要输出$NO$ #include<cmath> #include<cstdio> #include<cstring> #include<algorithm> #define N 1003 #define read(x) x = getint() using namespace std; inline int getint() { int k = 0, fh = 1; char c

poj 1228 Grandpa&#39;s Estate(凸包)

Grandpa's Estate Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 11508   Accepted: 3188 Description Being the only living descendant of his grandfather, Kamran the Believer inherited all of the grandpa's belongings. The most valuable one

POJ1228:Grandpa&#39;s Estate——题解

http://poj.org/problem?id=1228 题目大意:给一个凸包,问是否为稳定凸包. ———————————————————————— 稳定凸包的概念为:我任意添加一个点都不能使这个凸包得到扩充,这样的凸包为稳定凸包. 我们求完凸包后枚举边然后枚举有多少点在上面即可. (网上的程序真的大部分是错的……) #include<cstdio> #include<queue> #include<cctype> #include<cstring> #

POJ 1228 Grandpa&#39;s Estate [稳定凸包]

Grandpa's Estate Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 13112   Accepted: 3697 Description Being the only living descendant of his grandfather, Kamran the Believer inherited all of the grandpa's belongings. The most valuable one

poj 1228 Grandpa&#39;s Estate (稳定凸包问题)

---恢复内容开始--- 题意:你的长辈给你留了块土地,然而这块土地是以一些钉子来界定的,题目要做的就是给你一堆钉子的坐标(也就是凸包上部分的点),然后问你能不能唯一确定这块土地 //不得不说知道题意后一脸懵逼.. 知识:稳定凸包 所谓稳定就是判断能不能在原有凸包上加点,得到一个更大的凸包,并且这个凸包包含原有凸包上的所有点. 举一个不稳定的例子 为什么说它不稳定呢?因为他可以加点来得到更大的凸包 那什么样子是稳定的呢? 因此我们可以知道当一个凸包稳定时,凸包的每条边上都要有至少三个点,若只有两

POJ 1228 Grandpa&#39;s Estate --深入理解凸包

题意: 判断凸包是否稳定. 解法: 稳定凸包每条边上至少有三个点. 这题就在于求凸包的细节了,求凸包有两种算法: 1.基于水平序的Andrew算法 2.基于极角序的Graham算法 两种算法都有一个类似下面的语句: for(int i=0;i<n;i++) { while(m > 1 && Cross(ch[m-1]-ch[m-2], p[i]-ch[m-2]) <= 0) m--; ch[m++] = p[i]; } 这样的话,求出来就是最简凸包,即点数尽量少的凸包,因

Grandpa&#39;s Estate - POJ 1228(稳定凸包)

刚开始看这个题目不知道是什么东东,后面看了大神的题解才知道是稳定凸包问题,什么是稳定凸包呢?所谓稳定就是判断能不能在原有凸包上加点,得到一个更大的凸包,并且这个凸包包含原有凸包上的所有点.知道了这个东西就简单了,直接求出来凸包后,然后判断每条边上的点是否超过三点就行了. 代码如下: ============================================================================================================

【POJ】1228 Grandpa&#39;s Estate(凸包)

http://poj.org/problem?id=1228 随便看看就能发现,凸包上的每条边必须满足,有相邻的边和它斜率相同(即共线或凸包上每个点必须一定在三点共线上) 然后愉快敲完凸包+斜率判定,交上去wa了QAQ.原因是忘记特判一个地方....因为我们求的凸包是三点共线的凸包,在凸包算法中我们叉积判断只是>0而不是>=0,那么会有一种数据为所有点共线的情况,此时求出来的凸包上的点是>原来的点的(此时恰好符合答案NO,因为可以在这条线外随便点一个点就是一个凸包了...)然后特判一下.