码迷,mamicode.com
首页 > 其他好文 > 详细

POJ1228-Grandpa's Estate(凸包)

时间:2014-08-20 21:09:12      阅读:328      评论:0      收藏:0      [点我收藏+]

标签:des   style   color   os   io   strong   for   ar   

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(凸包)

标签:des   style   color   os   io   strong   for   ar   

原文地址:http://blog.csdn.net/mowayao/article/details/38711845

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!