Grandpa'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 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 |
[Submit] [Go Back] [Status] [Discuss]
题目大意:给出一个凸包的部分点,判断这些点能否唯一确定一个凸包,即凸包是稳定的。
题解:凸包
对于这些点求凸包(注意一条线上的点只保留端点),因为三点定一线,所以判断凸包的每条边上是否有除端点外的其他点,如果所有边都满足条件就输出YES,否则输出NO
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#define N 1003
#define eps 1e-7
using namespace std;
int m,n,vis[N];
struct vector{
double x,y;
vector (double X=0,double Y=0){
x=X,y=Y;
}
}p[N],ch[N];
typedef vector point;
vector operator -(vector a,vector b){
return vector (a.x-b.x,a.y-b.y);
}
bool operator ==(vector a,vector b){
return a.x==b.x&&a.y==b.y;
}
bool operator <(vector a,vector b)
{
return a.x<b.x||a.x==b.x&&a.y<b.y;
}
int dcmp(double x)
{
if (fabs(x)<eps) return 0;
return x<0?-1:1;
}
double cross(vector a,vector b)
{
return a.x*b.y-a.y*b.x;
}
void convexhull(point *p,int n,point *ch)
{
sort(p+1,p+n+1);
if (n==1){
ch[++m]=n;
return ;
}
m=1;
for (int i=1;i<=n;i++){
while (m>2&&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-1;i>=1;i--){
while (m>k&&dcmp(cross(ch[m-1]-ch[m-2],p[i]-ch[m-2]))<=0) m--;
ch[m++]=p[i];
}
m--;
}
double len(vector a)
{
return sqrt(a.x*a.x+a.y*a.y);
}
double dot(vector a,vector b)
{
return a.x*b.x+a.y*b.y;
}
double dists(point p,point a,point b)
{
if (a==b) return len(p-a);
vector v1=b-a,v2=p-a,v3=p-b;
if (dcmp(dot(v1,v2))<0) return len(p-a);
if (dcmp(dot(v1,v3))>0) return len(p-b);
return fabs(cross(v1,v2))/len(v1);
}
int main()
{
freopen("a.in","r",stdin);
// freopen("my.out","w",stdout);
int t=0;
scanf("%d",&t);
while (t--){
scanf("%d",&n);
for (int i=1;i<=n;i++) scanf("%lf%lf",&p[i].x,&p[i].y);
if (n<6) {
printf("NO\n");
continue;
}
convexhull(p,n,ch);
//for (int i=1;i<=m;i++)
//cout<<ch[i].x<<" "<<ch[i].y<<endl;
bool pd=true;
for (int i=1;i<=m-1;i++){
int t=0;
for (int j=1;j<=n;j++)
if (dcmp(dists(p[j],ch[i],ch[i+1]))==0)
t++;
//cout<<t<<endl;
if (t<3) {
pd=false;
break;
}
}
if (pd) printf("YES\n");
else printf("NO\n");
}
}