当前位置: 代码迷 >> 综合 >> UVA 10256 The Great Divide 凸包 .
  详细解决方案

UVA 10256 The Great Divide 凸包 .

热度:48   发布时间:2023-09-23 04:00:10.0

题目地址:https://vjudge.net/problem/UVA-10256

红点做一个凸包,蓝点做一个凸包,要判断他们之间能由直线隔开,也就是没有相交

有两种情况要注意:

UVA 10256 The Great Divide 凸包 .

UVA 10256 The Great Divide 凸包 .

所以

1两个凸包上任意两个线段不想交

2凸包上任意一个点不存在另一个凸包内部


#include <bits/stdc++.h>
using namespace std;
#define REP(i,a,b)  for(int i=a;i<=(int)(b);++i)
#define REPD(i,a,b) for(int i=a;i>=(int)(b);--i)
const double PI=acos(-1);
const double EPS=1e-6;
int dcmp(double x){if(fabs(x)<EPS) return 0;return x > 0 ? 1 : -1;
}
struct Point{double x,y;
};
typedef Point Vector;
bool operator < (const Point& p1, const Point& p2){	return p1.x<p2.x || (p1.x==p2.x && p1.y<p2.y) ;}
Vector operator / (const Point& A, double x){	return Vector{A.x/x, A.y/x};}
Vector operator * (const Vector& A, double x){	return Vector{A.x*x, A.y*x};}
Vector operator - (const Vector& A, const Vector& B){	return Vector{A.x-B.x,A.y-B.y};}
Vector operator + (const Point& A, const Vector& v){	return Point{A.x+v.x,A.y+v.y};}
Vector Rotate(const Point& p,double ang){	return Vector{p.x*cos(ang)-p.y*sin(ang),p.x*sin(ang)+p.y*cos(ang)};}
double Cross(const Vector& A, const 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;}
double Length(Vector v){	return sqrt(fabs(Dot(v,v)));}
Vector Unit(Vector v){	return v/Length(v);}const int maxn=500+5;
Point M[maxn],C[maxn],Mch[maxn],Cch[maxn];
int convexHull(Point* A, int n, Point* sol){sort(A,A+n);int m=0;REP(i,0,n-1){while(m>1 && dcmp( Cross(sol[m-1]-sol[m-2], A[i]-sol[m-1])) <= 0) m--;sol[m++]=A[i];}int k=m;REPD(i,n-2,0){while(m>k && dcmp( Cross(sol[m-1]-sol[m-2], A[i]-sol[m-1])) <= 0) m--;sol[m++]=A[i];}if(n>1) m--;return m;
}
bool OnSegment(Point p, Point A, Point B){return dcmp(Cross(A-p, B-p))==0 && dcmp(Dot(A-p, B-p))<=0;
}
bool isIntersection(Point p1, Point p2, Point p3, Point p4){if(OnSegment(p1,p3,p4)||OnSegment(p2,p3,p4)||OnSegment(p3,p1,p2)||OnSegment(p4,p1,p2)) return true;double c1=Cross(p2-p1,p3-p1), c2=Cross(p2-p1,p4-p1),c3=Cross(p4-p3,p1-p3), c4=Cross(p4-p3,p2-p3);return dcmp(c1)*dcmp(c2)<0 && dcmp(c3)*dcmp(c4)<0;
}
bool isPointInPolygon(Point A, Point* poly, int n){int wn=0;REP(i,0,n-1){int k=dcmp(Cross(poly[(i+1)%n]-poly[i], A-poly[i]));double d1=poly[i].y-A.y;double d2=poly[(i+1)%n].y-A.y;if(k>0 && dcmp(d1)<=0 && dcmp(d2)>0) wn++;if(k<0 && dcmp(d2)<=0 && dcmp(d1)>0) wn--;}if(wn!=0) return true;return false;
}
int main(int argc, char const *argv[])
{// freopen("input.in","r",stdin);int m,c;while(scanf("%d %d",&m,&c)==2&&m+c){REP(i,0,m-1) scanf("%lf%lf",&M[i].x,&M[i].y);REP(i,0,c-1) scanf("%lf%lf",&C[i].x,&C[i].y);int mCnt=convexHull(M,m,Mch);int cCnt=convexHull(C,c,Cch);bool ok=true;REP(i,0,mCnt-1) {REP(j,0,cCnt-1) if(isIntersection(Mch[i],Mch[(i+1)%mCnt],Cch[j],Cch[(j+1)%cCnt])) { ok=false; break; }if(!ok) break;}if(ok) {REP(i,0,mCnt-1){REP(j,0,cCnt-1) if(isPointInPolygon(Mch[i],Cch,cCnt)) {ok=false; break; }if(!ok) break;} }if(ok) {REP(i,0,cCnt-1){REP(j,0,mCnt-1) if(isPointInPolygon(Cch[i],Mch,mCnt)) {ok=false; break; }if(!ok) break;} }printf("%s\n", ok?"Yes":"No");}return 0;
}


  相关解决方案