zoukankan      html  css  js  c++  java
  • 计算几何:POJ1228 Grandpa's Estate 稳定凸包

    Grandpa's Estate
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 14454   Accepted: 4042

    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

    稳定凸包在我的博客:计算几何:凸包中已经略微讲过了。
    直接使用极角排序和Graham,以为可以把凸包上的所有点都记录下来,但经过一些尝试,发现一般的极角排序是无论如何也存不下所有点的,用以下的点来示例:(请在草稿纸上画图模拟)
    0,0
    0,1
    0,2
    2,2
    2,0
    1,0
    以顺时针,距离由远到近排序:
    前四个点的顺序:
    0,0
    2,0
    1,0
    2,2
    那么,(0,0)和(2,0)入队,由于意图存所有点,所以共线点入队,所以(1,0)入队,然后遇上点(2,2),显然点(1,0)被出队,没有存下所有点。
    以顺时针,距离由近到远排序:
    后三个点的顺序:
    2,2
    0,1
    0,2
    和上面的情况类似,(0,1)会被出队。
    因此,我们只能用最简的方式存凸包,再依个判断边上的点,由于题目数据并不变态,所以可以直接过。

    另外,题目有一个小坑点,你需要判断一下图形是多边形还是一条线。

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<cmath>
    #include<stack>
    #include<map>
    #include<vector>
    #include<set>
    #include<queue>
    using namespace std;
    const int MAXN=1e3+10;
    const double mod=1e16+7;
    const double PI=acos(-1.0);
    #define INF 0x7fffffff
    #define ll long long
    #define edl putchar('
    ')
    #define useit  ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    #define FOR(i,a,b) for(int i=a;i<=b;i++)
    #define ROF(i,a,b) for(int i=a;i>=b;i--)
    #define mst(a) memset(a,0,sizeof(a))
    #define mstn(a,n) memset(a,n,sizeof(a))
    #define eps 1e-8
    #define zero(x)(((x)>0?(x):-(x))<eps)
    struct point
    {
    	double x,y;
    };
    double xmult(point p1,point p2,point p0)
    {
        return(p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
    }
    double distan(point p1,point p2)
    {
        return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
    }
    int dots_inline(point p1,point p2,point p3)
    {
        return zero(xmult(p1,p2,p3));
    }
    double area(point *poi,int n)//求多边形面积,poi必须为顺时针或逆时针 
    {
    	double an=0;
    	FOR(i,0,n-2)
    	an+=((poi[i].x*poi[i+1].y)-(poi[i+1].x*poi[i].y));
    	an+=((poi[n-1].x*poi[1].y)-(poi[1].x*poi[n-1].y));
    	return fabs(an)/2;
    }
    
    point p[MAXN];
    int n,T;
    int sta[MAXN],top;
    
    bool Tro(point p1,point p2)//极角排序 
    {
    	int tmp=xmult(p[0],p1,p2);
    	if(tmp>0) return true;
    	else if(tmp==0&&distan(p[0],p1)<distan(p[0],p2)) return true;
    	else return false;
    }
    void Graham(int n)//求凸包 
    {
    	int i;
    	if(n==1){top=0;sta[0]=0;}
    	if(n==2)
    	{
    		top=1;
    		sta[0]=0;
    		sta[1]=1;
    	}
    	if(n>2)
    	{
    		for(int i=0;i<=1;i++) sta[i]=i;
    		top=1;
    		for(int i=2;i<n;i++)//O(2n) 求出前i个点集形成的凸包 
    		{
    			while(top>0&&xmult(p[sta[top-1]],p[sta[top]],p[i])<=0) 
    				top--;
    			top++;
    			sta[top]=i;
    		}	
    	}
    }
    int main()
    {
    	scanf("%d",&T);
    	while(T--)
    	{
    		double ans=0,l,minn=INF,miny=INF;
    		int p0=0,flag=0;
    		cin>>n;
    		FOR(i,0,n-1)
    		{
    			cin>>p[i].x>>p[i].y;
    			if(p[i].x<minn)
    			{
    				minn=p[i].x;
    				p0=i;
    				miny=p[i].y;
    			}
    			else if(p[i].x==minn&&p[i].y<miny)
    			{
    				p0=i;
    				miny=p[i].y;
    			}
    		}
    		swap(p[0],p[p0]);
    		sort(p+1,p+n,Tro);
    		Graham(n);
    		if(top<=1)
    		flag=1;
    		else
    		FOR(i,0,top)
    		{
    			int mark=0;
    			point a=p[sta[i]],b=p[sta[(i+1)%(top+1)]];
    			FOR(i,0,n-1)
    			{
    				if(p[i].x==a.x&&p[i].y==a.y)continue;
    				if(p[i].x==b.x&&p[i].y==b.y)continue;
    				if(dots_inline(a,b,p[i]))
    				{
    					mark=1;
    					break;
    				}
    			}
    			if(!mark)flag=1;
    			if(flag)break;
    		}
    		if(flag) cout<<"NO"<<endl;
    		else cout<<"YES"<<endl;
    	 } 
    } 
    

      

  • 相关阅读:
    layer备忘
    Java中遍历Map对象的4种方法
    为什么Java中1000==1000为false而100==100为true?
    linux系统安装psycopg2
    centos7源码安装mysql5.7
    Azure Sql
    javascript和jQuery动态修改css样式的方法
    Git early EOF index-pack failed 问题
    C# 多线程——SemaphoreSlim的使用
    Docker 可视化
  • 原文地址:https://www.cnblogs.com/qq936584671/p/7954881.html
Copyright © 2011-2022 走看看