poj1228 (判断凸包的完整性)

来源:互联网 发布:java可变参数怎么接收 编辑:程序博客网 时间:2024/06/05 01:05
Grandpa's Estate
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 11185 Accepted: 3081

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

16 0 01 23 42 02 4 5 0

Sample Output

NO

题意是个你一个凸包上的点。问这个凸包是否是唯一存在的。也就是说并不能加点构成更大的凸包。

三点确定一条直线。我们将这些凸包上的点按照极角排序。最后一条边要注意处理一下,不然会出问题的。还是有这个小细节需要处理。然后判断同一条线上是否至少有三个点。

#include<limits>#include<queue>#include<vector>#include<list>#include<map>#include<set>#include<deque>#include<stack>#include<bitset>#include<algorithm>#include<functional>#include<numeric>#include<utility>#include<sstream>#include<iostream>#include<iomanip>#include<cstdio>#include<cmath>#include<cstdlib>#include<cstring>#include<ctime>#define LL __int64#define eps 1e-8#define pi acos(-1)using namespace std;struct point{double x,y;point (double x=0,double y=0):x(x),y(y){}};point p[1010];int dcmp(double x){if (fabs(x)<=eps) return 0;if (x>0) return 1;else return -1;}double across(point a,point b,point c){return (a.x-c.x)*(b.y-c.y)-(a.y-c.y)*(b.x-c.x);}double dis(point a,point b){return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); }bool cmp(point a,point b){int  k=dcmp(across(a,b,p[0]));if (k==-1) return false;if (k==0) return dis(a,p[0])<dis(b,p[0]);return true;}int main(){int T,n;scanf("%d",&T);while (T--){scanf("%d",&n);int k=0;for  (int i=0;i<n;i++){scanf("%lf%lf",&p[i].x,&p[i].y);if (p[k].y>p[i].y || (p[k].y==p[i].y && p[k].x>p[i].y))k=i;}if (n<6){cout<<"NO"<<endl;continue;}point t=p[k];p[k]=p[0];p[0]=t;sort(p+1,p+n,cmp);int j;for (j=n-2;j>=0;j--)if (dcmp(across(p[0],p[n-1],p[j]))!=0) break;j++;for (int i=j;i<=(n-1+j)/2;i++){t=p[i];p[i]=p[n-1-i+j];p[n-1-i+j]=t;}p[n]=p[0];point a=p[0],b=p[1];int flag=0;int ans=1;int tt=0;for (int i=2;i<=n;i++){int k=dcmp(across(a,b,p[i]));if (k==0){flag=1;}else{tt++;if (!flag){ans=0;break;}else{a=p[i-1];b=p[i];flag=0;}}}if (ans && tt && flag) cout<<"YES"<<endl;else cout<<"NO"<<endl;}return 0;}


0 0
原创粉丝点击