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
其实判断稳定凸包就是判断一条线上是不是有三个点共线
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<algorithm>
using namespace std;
int n,top;
struct node
{
int x,y;
} a[1005],p[1005];
double dis(node p1,node p2)
{
return sqrt((p2.x-p1.x)*(p2.x-p1.x)+(p2.y-p1.y)*(p2.y-p1.y));
}
double cross(node p0,node p1,node p2)
{
return (p1.x-p0.x)*(p2.y-p0.y)-(p1.y-p0.y)*(p2.x-p0.x);
}
bool cmp(node p1,node p2)
{
double z=cross(a[0],p1,p2);
if(z>0||(z==0&&dis(a[0],p1)<dis(a[0],p2)))
return 1;
return 0;
}
void Graham()
{
int k=0;
for(int i=0; i<n; i++)
{
if(a[i].y<a[k].y||(a[i].y==a[k].y&&a[i].x<a[k].x))
k=i;
}
swap(a[0],a[k]);
sort(a+1,a+n,cmp);
p[0]=a[0];
p[1]=a[1];
top=2;
for(int i=2; i<n; i++)
{
if(top>1&&cross(p[top-2],p[top-1],a[i])<0)
top--;
p[top++]=a[i];
}
}
int solve()
{
for(int i=1; i<top-1; i++)//这里WA了好几发,凸包的点数组p只有0~top-1
{
if(cross(p[i],p[i+1],p[i+2])!=0&&cross(p[i-1],p[i],p[i+1])!=0)
return 0;
}
return 1;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(int i=0; i<n; i++)
scanf("%d%d",&a[i].x,&a[i].y);
if(n<6)//当N小于6时,不可能有稳定凸包,一条线上不可能有3个点
printf("NO\n");
else
{
Graham();
// for(int i=0;i<top;i++)
// printf("%d%d\n",p[i].x,p[i].y);
if(solve())
printf("YES\n");
else
printf("NO\n");
}
}
}