题目描述:
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
看了大牛的博客,发现讲解很详细,就直接附上网址喽~
http://www.cnblogs.com/xdruid/archive/2012/06/20/2555536.html
附上我的ac代码:
#include<iostream>
#include<cmath>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn=1000+5;
struct Point
{
int x,y;
Point(int x=0,int y=0): x(x),y(y) {}
};
Point p[maxn],ch[maxn];
typedef Point Vector;
bool operator <(const Point& a,const Point& b)
{
return a.x<b.x||(a.x==b.x&&a.y<b.y);
}
int Dot(Vector A,Vector B)
{
return A.x*B.y-A.y*B.x;
}
bool operator ==(const Point& a,const Point& b)
{
return a.x==b.x&&a.y==b.y;
}
bool operator !=(const Point& a,const Point& b)
{
return a.x!=b.x||a.y!=b.y;
}
Vector operator - (Point A,Point B)
{
return Vector(A.x-B.x,A.y-B.y);
}
int ConvextHull(int n)
{
sort(p,p+n);
int m=0;
for(int i=0;i<n;i++)
{
while(m>1&&Dot(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&&Dot(ch[m-1]-ch[m-2],p[i]-ch[m-2])<=0) m--;
ch[m++]=p[i];
}
if(n>1) m--;
return m;
}
int main()
{
//freopen("in.txt","r",stdin);
int cas;
scanf("%d",&cas);
while(cas--)
{
memset(ch,0,sizeof(ch));
int n;
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%d%d",&p[i].x,&p[i].y);
int m=ConvextHull(n);
bool flag=true;
for(int i=0;i<m-1&&flag;i++)
{
int have=0;
for(int j=0;j<n&&flag;j++)
{
if(p[j]!=ch[i]&&p[j]!=ch[i+1])
{
if(Dot(p[j]-ch[i],ch[i+1]-ch[i])==0)
have++;
}
}
if(have<1)
{
flag=false;
break;
}
}
if(flag)
{
int have=0;
for(int j=0;j<n;j++)
{
if(p[j]!=ch[m-1]&&p[j]!=ch[0])
{
if(Dot(p[j]-ch[m-1],ch[m-1]-ch[0])==0)
{
have++;
break;
}
}
}
if(have<1)
flag=false;
}
if(n<6)
flag=false;
if(flag)
printf("YES\n");
else
printf("NO\n");
}
}