zoukankan      html  css  js  c++  java
  • UVA 10256 The Great Divide(点在多边形内)

    The Great Divid

    【题目链接】The Great Divid

    【题目类型】点在多边形内

    &题解:

    蓝书274, 感觉我的代码和刘汝佳的没啥区别,可是我的就是wa,所以贴一发刘汝佳的吧.
    感觉这题最好的地方就是让我大致懂了点在多边形内的判断,写的好神奇,没有做一条直线,而是2个if判断就替代了这个,好腻害

    &代码:

    #include <cstdio>
    #include <vector>
    #include <cmath>
    #include <algorithm>
    using namespace std;
    
    const double eps = 1e-10;
    int dcmp(double x) {if(fabs(x)<eps) return 0; return x<0?-1:1;}
    struct Point {
    	double x,y;
    	Point(double x=0,double y=0):x(x),y(y) {}
    };
    typedef Point Vector;
    Vector operator - (Vector A,Vector B) {return Vector(A.x-B.x , A.y-B.y); }
    double Cross(Vector A,Vector B) {return A.x*B.y-A.y*B.x;}
    double Dot(Vector A,Vector B) {return A.x*B.x+A.y*B.y;}
    bool operator == (Point a,Point b) {return dcmp(a.x-b.x)==0&&dcmp(a.y-b.y)==0;}
    bool operator < (Point a,Point b) {return a.x<b.x||(a.x==b.x&&a.y<b.y);}
    
    bool SegmentProperIntersection(const Point& a1,const Point& a2,const Point& b1,const Point& b2) {
    	double c1=Cross(a2-a1 , b1-a1), c2=Cross(a2-a1 , b2-a1);
    	double c3=Cross(b2-b1 , a1-b1), c4=Cross(b2-b1 , a2-b1);
    	return dcmp(c1)*dcmp(c2)<0 && dcmp(c3)*dcmp(c4)<0;
    }
    bool OnSegment(const Point& p,const Point& a1,const Point& a2) {
    	return dcmp(Cross(a1-p , a2-p))==0 && dcmp(Dot(a1-p , a2-p))<0;
    }
    
    vector<Point> ConvexHull(vector<Point> p) {
    	sort(p.begin(),p.end());
    	p.erase(unique(p.begin(), p.end()), p.end());
    	int n=p.size();
    	int m=0;
    	vector<Point> ch(n+1);
    	for(int i=0; i<n; i++) {
    		while(m>1&&Cross(ch[m-1]-ch[m-2],p[i]-ch[m-2])<=0) m--;
    		ch[m++]=p[i];
    	}
    	int k=m;
    	for(int i=n-2; i>=0; i--) {
    		while(m>k&&Cross(ch[m-1]-ch[m-2],p[i]-ch[m-2])<=0) m--;
    		ch[m++]=p[i];
    	}
    	if(n>1) m--;
    	ch.resize(m);
    	return ch;
    }
    
    int IsInPolygon(const Point& p,const vector<Point>& poly) {
    	int n = poly.size() , wn=0;
    	for(int i=0; i<n; i++) {
    		const Point& p1 = poly[i] , p2 = poly[(i+1)%n];
    		if(p1==p || p2==p || OnSegment(p, p1, p2)) return -1;
    		int k=dcmp(Cross(p2-p1 , p-p1));
    		int d1=dcmp(p1.y - p.y) , d2=dcmp(p2.y - p.y);
    		if(k>0 && d1<=0 && d2>0) wn++;
    		if(k<0 && d2<=0 && d1>0) wn--;
    	}
    	return wn?1:0;
    }
    bool ConvexPolygonDisjoint(const vector<Point> ch1,const vector<Point> ch2) {
    	int c1=ch1.size(), c2=ch2.size();
    	for(int i=0; i<c1; i++) {
    		if(IsInPolygon(ch1[i],ch2)) return false;
    	}
    	for(int i=0; i<c2; i++) {
    		if(IsInPolygon(ch2[i],ch1)) return false;
    	}
    	for(int i=0; i<c1; i++) {
    		for(int j=0; j<c2; j++) {
    			if(SegmentProperIntersection(ch1[i],ch1[(i+1)%c1],ch2[j],ch2[(j+1)%c2])) return false;
    		}
    	}
    	return true;
    }
    int main() {
    	freopen("e:1.in","r",stdin);
    	int n,m;
    	while(scanf("%d%d",&n,&m)==2&&n>0&&m>0) {
    		vector<Point> P1,P2;
    		double x,y;
    		for(int i=0; i<n; i++) {
    			scanf("%lf%lf",&x,&y);
    			P1.push_back(Point(x,y));
    		}
    		for(int i=0; i<m; i++) {
    			scanf("%lf%lf",&x,&y);
    			P2.push_back(Point(x,y));
    		}
    		if(ConvexPolygonDisjoint(ConvexHull(P1), ConvexHull(P2)))
    			printf("Yes
    ");
    		else
    			printf("No
    ");
    	}
    }
    
  • 相关阅读:
    Working with Deployment Configurations in CodeDeploy
    ECS 容器实例生命周期
    设置 API Gateway 金丝雀版本部署
    Elastic Beanstalk 滚动环境配置更新
    Kinesis Data Firehose 中的数据保护
    为 API Gateway REST API 资源启用 CORS
    高级 AWS Elastic Beanstalk 环境配置
    Amazon SWF Actors
    AWS CloudFormation 模板结构
    字符编码
  • 原文地址:https://www.cnblogs.com/s1124yy/p/6782903.html
Copyright © 2011-2022 走看看