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
此题的思路就是如果每条凸包的边,如果原来有3个点在在边上,那么凸包唯一确定
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
struct Point
{
int x,y;
}point[1008],res[1008];
bool cmp(Point a,Point b)
{
if(a.x>b.x)return 0;
else if(a.x==b.x)return a.y>b.y?0:1;
return 1;
}
bool ral(Point p1,Point p2,Point p3)
{
return ((p3.y-p1.y)*(p2.x-p1.x)>(p2.y-p1.y)*(p3.x-p1.x));
}
bool operator != (Point a,Point b)
{
if(a.x==b.x&&a.y==b.y)
return 0;
return 1;
}
bool biandian(Point a,Point b,Point c)
{
if((c.y-a.y)*(b.x-c.x)==(b.y-c.y)*(c.x-a.x))
{
return 1;
}
return 0;
}
int main()
{
int t,n;
cin>>t;
while(t--)
{
cin>>n;
for(int i=0;i<n;i++)
{
cin>>point[i].x>>point[i].y;
}
sort(point,point+n,cmp);
int m=-1;
for(int i=0;i<n;i++)
{
while(m>=1&&!ral(res[m-1],res[m],point[i]))
{
m--;
}
res[++m]=point[i];
}
int k=m;
for(int i=n-1;i>=0;i--)
{
while(m>k&&!ral(res[m-1],res[m],point[i]))
{
m--;
}
res[++m]=point[i];
}
bool flag=true;
for(int i=0;i<m;i++)
{
int hehe=0;
for(int j=0;j<n;j++)
{
if(biandian(res[i],point[j],res[i+1]))
{
hehe++;
}
if(hehe>=3)
{
break;
}
}
if(hehe<3)
{
flag=false;
break;
}
}
if(n<=5||!flag)
{
cout<<"NO"<<endl;
}
else cout<<"YES"<<endl;
}
return 0;
}