zoukankan      html  css  js  c++  java
  • POJ 1228 Grandpa's Estate

    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



    需要考虑最后没有生成凸包的情况,此时为NO

    #include<algorithm>
    #include<iostream>
    #include<math.h>
    using namespace std;
    
    typedef struct{
        double x,y;
    } Point;
    
    Point p[1005];
    int ans[1005];
    int t,n;
    
    bool cmp(Point a,Point b){
        return a.y<b.y||(a.y==b.y&&a.x<b.x);
    }
    
    double cross(int i,int j,int k){
        Point a=p[i];
        Point b=p[j];
        Point c=p[k];
        return (c.x-b.x)*(a.y-b.y)-(c.y-b.y)*(a.x-b.x); 
    }
    
    int Q[1005],top;
    void scan(){
        sort(p+1,p+n+1,cmp);
        Q[1]=1;Q[2]=2;top=2;
        for(int i=3;i<=n;++i)
        {
            while(top>1&&cross(Q[top-1],Q[top],i)<=0) top--;
            Q[++top]=i;
        }
        
        for(int i=1;i<=top;++i)
        ans[i]=Q[i];
        ans[0]=top;
        
        Q[1]=n;Q[2]=n-1;top=2;
        for(int i=n-2;i>=1;--i)
        {
            while(top>1&&cross(Q[top-1],Q[top],i)<=0) top--;
            Q[++top]=i;
        }
        
        for(int i=2;i<top;++i)
        ans[++ans[0]]=Q[i];
    }
    
    int main()
    {
        //freopen("fc.in","r",stdin);
        //freopen("fc.out","w",stdout);
        
        cin>>t;
        while(t>0)
        {
            t--;
            cin>>n;
            for(int i=1;i<=n;++i)
            cin>>p[i].x>>p[i].y;
            scan();
            
            if(ans[0]<3) {cout<<"NO"<<endl;continue;}
            
            
            ans[0]++;
            ans[ans[0]]=1;
            int i;
            for(i=1;i<ans[0];++i)
            {    
                int j; 
                for(j=1;j<=n;++j)
                if(j!=ans[i]&&j!=ans[i+1]&&fabs(cross(ans[i],ans[i+1],j))<1e-6)
                break;
                if(j>n) break;
            }
            if(i<ans[0]) cout<<"NO"<<endl;
            else cout<<"YES"<<endl;
        
        }
    }
     
  • 相关阅读:
    深入理解 RPC
    Redis 深度历险
    tomcat性能优化
    高性能IO之Reactor模式
    常用sql语句整理[SQL Server]
    SQLite遇到的关于x64、x86问题
    C#和Javascript的try…catch…finally的一点差别
    C#写Windows服务
    Windows_7_休眠按钮没有了_如何找回?
    XmlReader读取XML
  • 原文地址:https://www.cnblogs.com/noip/p/9518375.html
Copyright © 2011-2022 走看看