1. 程式人生 > >P - Grandpa's Estate POJ - 1228 (凸包)

P - Grandpa's Estate POJ - 1228 (凸包)

P - Grandpa's Estate

 POJ - 1228 

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<cstdio>
#include<stack>
#include<set>
#include<vector>
#include<queue>
#include<algorithm>
#include<cstring>
#include<string>
#include<map>
#include<iostream>
#include<cmath>
using namespace std;
#define inf 0x3f3f3f3f
typedef long long ll;
const int N=110;
const int nmax = 1020;
const double esp = 1e-9;
const double PI=3.1415926;
int n;
struct point
{
    int x,y;
    point()
    {
    }
    point(int _x,int _y):x(_x),y(_y)
    {
    }
    point operator -(const point &b)const
    {
        return point(x-b.x,y-b.y);
    }
} p[nmax],p0;
int tubao[nmax];
double cross(point p1,point p2)
{
    return p1.x*p2.y-p1.y*p2.x;
}
double dis(point p1,point p2)
{
    return sqrt((1.0*p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
}
bool cmp(point p1,point p2)
{
    double area=cross(p1-p0,p2-p0);
    if(fabs(area)<esp)
    {
        return dis(p1,p0)<dis(p2,p0);
    }
    if(area>esp)
        return true;
    return false;
}
void solve()
{
    int top=1;
    tubao[0]=0;
    tubao[1]=1;
    for(int i=2; i<n; i++)
    {
        while(top>0&&cross(p[tubao[top]]-p[tubao[top-1]],p[i]-p[tubao[top-1]])<0)
            //使凸包上同一條邊3個以上點存在
            top--;
        tubao[++top]=i;
    }
    int flag=0;
    int s=0;
    tubao[++top]=0;
    for(int i=1; i<top; i++)
    {
        if(fabs(cross(p[tubao[i+1]]-p[tubao[i]],p[i]-p[tubao[s]]))<esp)  
        {
            flag=1;  //同一條線上多有兩個以上的點
        }
        else if(flag==1)
        {
            s=i;
            flag=0;
        }
        else
        {
            flag=-1;
            break;
        }
    }
    if(flag==-1)
        printf("NO\n");
    else
        printf("YES\n");
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(int i=0; i<n; i++)
            scanf("%d%d",&p[i].x,&p[i].y);
        if(n<6)  //5個點以內不可能形成每條邊有3個以上的點的凸包
        {
            printf("NO\n");
            continue;
        }
        int flag=0;
        for(int i=1; i<n-1; i++)
        {
            if(fabs(cross(p[i]-p[i-1],p[i+1]-p[i]))>esp)//判斷是否可以形成凸包,即不能都同線
            {
                flag=1;  
                break;
            }
        }
        if(flag==0)
        {
            printf("NO\n");
            continue;
        }
        p0.y=inf;
        p0.x=0;
        int k=0;
        for(int i=1; i<n; i++)
        {
            if(p[i].y<p0.y||(p[i].y==p0.y&&p[i].x<p0.x))
            {
                p0=p[i];
                k=i;
            }
        }
        if(k)
        {
            point p1=p[k];
            p[k]=p[0];
            p[0]=p1;
        }
        sort(p+1,p+n,cmp);
        solve();
    }
    return 0;
}