码迷,mamicode.com
首页 > 其他好文 > 详细

poj1228 Grandpa's Estate 凸包

时间:2015-08-07 22:27:41      阅读:103      评论:0      收藏:0      [点我收藏+]

标签:

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
Source

Tehran 2002 Preliminary

输入一个凸包上的点(没有凸包内部的点,要么是凸包顶点,要么是凸包边上的点),判断这个凸包是否稳定。所谓稳
定就是判断能不能在原有凸包上加点,得到一个更大的凸包,并且这个凸包包含原有凸包上的所有点。

分析:容易知道,当一个凸包稳定时,凸包的每条边上都要有至少三个点,若只有两个点,则可以增加一个点,得到更大的凸
包。这样我们可以求出凸包,在求凸包时把共线的点也加进来,这样我们就判断是否有连续的三点共线即可
注意:全部点都共线的情况输出是0
直接套上凸包模板。。。把点共线情况也加入凸包中。。。判断是否存在一点不与两个点共线。。。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
using namespace std;
#define MAX 1005
#define INF 1000000000

int n,L;
struct point  //构造点
{
    int x,y;
}p[MAX];

point save[1005];  //存点

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(const point& a,const point &b)
{
    if(a.y==b.y)return a.x<b.x;
    return a.y<b.y;
}
int Graham(point *p,int n)
{
    int i;
    sort(p,p+n,cmp);
    save[0]=p[0];
    save[1]=p[1];
    int top = 1;
    for(i=0;i<n;i++)
    {
        while(top&&xmult(save[top],p[i],save[top-1])>0) top--;//去掉=就是包含共线的凸包。。
        save[++top]=p[i];
    }
    int mid=top;
    for(i=n-2;i>=0;i--)
    {
        while(top>mid&&xmult(save[top],p[i],save[top-1])>0) top--;//去掉=就是包含共线的凸包。。
        save[++top]=p[i];
    }
    return top;
}
//求凸包。。。
int tot;
bool judge(int i)
{
    int k1=(save[(i+1)%tot].x-save[i%tot].x)*(save[(i-1+tot)%tot].y-save[(i)%tot].y);
    int k2=(save[(i-1+tot)%tot].x-save[(i)%tot].x)*(save[(i+1)%tot].y-save[i%tot].y);
    if(k1==k2) return true;
    return false;
}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        int i;
        for(i=0;i<n;i++) scanf("%d %d",&p[i].x,&p[i].y);
        tot=Graham(p,n);
        bool flag=true;
        int gg=0;
        for(i=0;i<tot;i++)
        {
            if(judge(i)==false)
            {
                if(judge(i+1)==false)
                {
                    flag=false;
                    break;
                }
                gg++;
            }
        }
        if(flag==true&&gg!=0) printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

poj1228 Grandpa's Estate 凸包

标签:

原文地址:http://blog.csdn.net/xtulollipop/article/details/47344791

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!