您的位置:首页 > 其它

poj1228 Grandpa's Estate 凸包

2015-08-07 20:49 381 查看
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

Tehran 2002 Preliminary

输入一个凸包上的点(没有凸包内部的点,要么是凸包顶点,要么是凸包边上的点),判断这个凸包是否稳定。所谓稳

定就是判断能不能在原有凸包上加点,得到一个更大的凸包,并且这个凸包包含原有凸包上的所有点。

分析:容易知道,当一个凸包稳定时,凸包的每条边上都要有至少三个点,若只有两个点,则可以增加一个点,得到更大的凸

包。这样我们可以求出凸包,在求凸包时把共线的点也加进来,这样我们就判断是否有连续的三点共线即可

注意:全部点都共线的情况输出是0

直接套上凸包模板。。。把点共线情况也加入凸包中。。。判断是否存在一点不与两个点共线。。。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
using namespace std;
#define MAX 1005
#define INF 1000000000

int n,L;
struct point  //构造点
{
int x,y;
}p[MAX];

point save[1005];  //存点

int xmult(point p1,point p2,point p0)
{
return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
}
bool cmp(const point& a,const point &b)
{
if(a.y==b.y)return a.x<b.x;
return a.y<b.y;
}
int Graham(point *p,int n)
{
int i;
sort(p,p+n,cmp);
save[0]=p[0];
save[1]=p[1];
int top = 1;
for(i=0;i<n;i++)
{
while(top&&xmult(save[top],p[i],save[top-1])>0) top--;//去掉=就是包含共线的凸包。。
save[++top]=p[i];
}
int mid=top;
for(i=n-2;i>=0;i--)
{
while(top>mid&&xmult(save[top],p[i],save[top-1])>0) top--;//去掉=就是包含共线的凸包。。
save[++top]=p[i];
}
return top;
}
//求凸包。。。
int tot;
bool judge(int i)
{
int k1=(save[(i+1)%tot].x-save[i%tot].x)*(save[(i-1+tot)%tot].y-save[(i)%tot].y);
int k2=(save[(i-1+tot)%tot].x-save[(i)%tot].x)*(save[(i+1)%tot].y-save[i%tot].y);
if(k1==k2) return true;
return false;
}

int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
int i;
for(i=0;i<n;i++) scanf("%d %d",&p[i].x,&p[i].y);
tot=Graham(p,n);
bool flag=true;
int gg=0;
for(i=0;i<tot;i++)
{
if(judge(i)==false)
{
if(judge(i+1)==false)
{
flag=false;
break;
}
gg++;
}
}
if(flag==true&&gg!=0) printf("YES\n");
else printf("NO\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: