X-man

导航

poj 1228 Grandpa's Estate

#include<stdio.h>
#include<algorithm>
using namespace std;
#define Max 1000
struct Point
{
    int x,y;
}p[Max],res[Max];
int xmult(Point p1,Point p2,Point p0)
{
    return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
}
bool cmp(Point p1,Point p2)
{
    if(p1.y<p2.y||p1.y==p2.y&&p1.x<p2.x)
    return true;
    return false;
}
int Graham(Point *p,int n)
{
    int i,len,top=1;
    sort(p,p+n,cmp);
    if(n==0)
    {
        return 0;
    }
    res[0]=p[0];
    if(n==1)
    {
        return 1;
    }
    res[1]=p[1];
    if(n==2)
    {
        return 2;
    }
    res[2]=p[2];
    for(i=2;i<n;i++)
    {
        while(top&&xmult(res[top],p[i],res[top-1])<0)
        {
            top--;
        }
        res[++top]=p[i];
    }
    len=top;//判断是否只有一半
    res[++top]=p[n-2];
    for(i=n-3;i>=0;i--)
    {
        while(len!=top&&xmult(res[top],p[i],res[top-1])<0)
        {
            top--;
        }
        res[++top]=p[i];
    }
    return top;
}
int main()
{
    int _case,n,i;
    scanf("%d",&_case);
    while(_case--)
    {
        scanf("%d",&n);
        for(i=0;i<n;i++)
            scanf("%d%d",&p[i].x,&p[i].y);
        n=Graham(p,n);
        int j=0,bj=0,ji=1;
        res[n++]=res[0];
        res[n++]=res[1];
        for(i=1;i<n;i++)
        {
            if(!xmult(res[i],res[i+1],res[j]))
            {
                bj++;
                while(!xmult(res[i],res[i+1],res[j]))i++;
                j=i;
                if(j!=n-2)ji++;
                //printf()
                //printf("#%d %d\n",ji,bj);
            }
            else break;
        }
        //printf("#%d %d\n",ji,bj);
        if(ji>=3&&bj==ji)
            printf("YES\n");
        else
            printf("NO\n");
    }
    return 0;
}

 

 

posted on 2013-08-04 11:58  雨钝风轻  阅读(176)  评论(0编辑  收藏  举报