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

POJ 1228 Grandpa's Estate [稳定凸包]

时间:2017-01-28 19:37:39      阅读:260      评论:0      收藏:0      [点我收藏+]

标签:strong   心情不好   arc   uniq   false   nes   ram   cto   int   

Grandpa‘s Estate
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 13112   Accepted: 3697

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


题意:输入一个凸包上的点(没有凸包内部的点,要么是凸包顶点,要么是凸包边上的点),判断这个凸包是否稳定。所谓稳

定就是判断能不能在原有凸包上加点,得到一个更大的凸包,并且这个凸包包含原有凸包上的所有点。


 

就是求凸包时把共线的点也加入凸包然后判断相邻两个连续三个点是不是至少有一个是共线的如果稳定那么必定有一个是共线还有一个特判至少六个点才行哦明白了吧我现在心情不好因为刚刚钥匙串出问题了网络出问题了所以凑合着看吧
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;
typedef long long ll;
const int N=1005;
const double eps=1e-8;
const double pi=acos(-1);

inline int read(){
    char c=getchar();int x=0,f=1;
    while(c<0||c>9){if(c==-)f=-1; c=getchar();}
    while(c>=0&&c<=9){x=x*10+c-0; c=getchar();}
    return x*f;
}

inline int sgn(double x){
    if(abs(x)<eps) return 0;
    else return x<0?-1:1;
}

struct Vector{
    double x,y;
    Vector(double a=0,double b=0):x(a),y(b){}
    bool operator <(const Vector &a)const{
        //return x<a.x||(x==a.x&&y<a.y);
        return sgn(x-a.x)<0||(sgn(x-a.x)==0&&sgn(y-a.y)<0);
    }
};
typedef Vector Point;
Vector operator +(Vector a,Vector b){return Vector(a.x+b.x,a.y+b.y);}
Vector operator -(Vector a,Vector b){return Vector(a.x-b.x,a.y-b.y);}
Vector operator *(Vector a,double b){return Vector(a.x*b,a.y*b);}
Vector operator /(Vector a,double b){return Vector(a.x/b,a.y/b);}
bool operator ==(Vector a,Vector b){return sgn(a.x-b.x)==0&&sgn(a.y-b.y)==0;}

double Dot(Vector a,Vector b){return a.x*b.x+a.y*b.y;}
double Cross(Vector a,Vector b){return a.x*b.y-a.y*b.x;}
int ConvexHull(Point p[],int n,Point ch[]){
    sort(p+1,p+1+n);
    int m=0;
    for(int i=1;i<=n;i++){
        while(m>1&&sgn(Cross(ch[m]-ch[m-1],p[i]-ch[m-1]))<0) m--;
        ch[++m]=p[i];
    }
    int k=m;
    for(int i=n-1;i>=1;i--){
        while(m>k&&sgn(Cross(ch[m]-ch[m-1],p[i]-ch[m-1]))<0) m--;
        ch[++m]=p[i];
    }
    if(n>1) m--;
    return m;
}

int n,r,x,y;
double ans;
Point p[N],ch[N];
bool solve(){
    for(int i=2;i<=n;i++)
        if(sgn(Cross(ch[i]-ch[i-1],ch[i%n+1]-ch[i]))!=0
           &&sgn(Cross(ch[i%n+1]-ch[i],ch[(i+1)%n+1]-ch[i%n+1]))!=0) return false;
    return true;
}
int main(int argc, const char * argv[]) {
    int T=read();
    while(T--){
        n=read();
        for(int i=1;i<=n;i++) p[i].x=read(),p[i].y=read();
        if(n<6) {puts("NO");continue;}
        n=ConvexHull(p,n,ch);
        if(solve()) puts("YES");else puts("NO");
    }
    return 0;
}

 

POJ 1228 Grandpa's Estate [稳定凸包]

标签:strong   心情不好   arc   uniq   false   nes   ram   cto   int   

原文地址:http://www.cnblogs.com/candy99/p/6354409.html

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