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<cmath> #include<cstdio> #include<cstdlib> #include<cstring> #include<iostream> #include<algorithm> using namespace std; const int maxn=1010; const double pi=acos(-1.0); const double eps=1e-6; struct Cpoint { double x,y; Cpoint(){} Cpoint(double xx,double yy):x(xx),y(yy){} Cpoint friend operator -(Cpoint a,Cpoint b){ return Cpoint(a.x-b.x, a.y-b.y); } double friend operator ^(Cpoint a,Cpoint b){ return a.x*b.y-b.x*a.y; } bool friend operator <(Cpoint a,Cpoint b){ if(a.y==b.y) return a.x<b.x; return a.y<b.y; } }; double dist(Cpoint a,Cpoint b) { return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); } int Sign(double x) { if(x>=-eps&&x<=eps) return 0; if(x>eps) return 1; return -1; } int N; Cpoint P[maxn]; bool cmp(Cpoint a,Cpoint b) { int s=Sign((a-P[1])^(b-P[1])); if(s>0||(s==0&&dist(a,P[1])<dist(b,P[1]))) return true; return false; } int find(Cpoint a,Cpoint b) { int res=0; for(int i=1;i<=N;i++){ if(Sign((P[i].x-a.x)*(P[i].x-b.x))>0) continue; if(Sign((P[i].y-a.y)*(P[i].y-b.y))>0) continue; if(Sign((b-a)^(P[i]-a))==0) res++; } return res; } bool Graham() { if(N<6) return false;//肯定不构成凸包 sort(P+1,P+N+1); //得到“原点 ” sort(P+2,P+N+1,cmp); //得到积角序 int q[maxn],top=2; q[1]=1; q[2]=2; //q[3]=3; for(int i=3;i<=N;i++){ while(top>1&&Sign((P[q[top]]-P[q[top-1]])^(P[i]-P[q[top]]))<=0) top--; q[++top]=i; } if(top<3) return false; //不构成凸包 for(int i=1;i<top;i++) if(find(P[q[i]],P[q[i+1]])<3) return false; if(find(P[q[top]],P[q[1]])<3) return false; return true; } int main() { int T;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(Graham()) printf("YES "); else printf("NO "); }return 0; }