UVA10256 The Great Divide(凸包相交)
原题链接:UVA10256 The Great Divide
题意:平面上有n个红点和m个蓝点,是否存在一条直线使得任取一个红点和一个蓝点都在直线的异侧?这条直线不能穿过红点或蓝点
分析:显然,求红点凸包和蓝点凸包,如果这两个凸包有相交的部分就不存在这样的直线,否则就存在呗
#include <bits/stdc++.h> using namespace std; double eps=1e-15; double pi=acos(-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);} 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);} int dcmp(double x){ if(fabs(x)<eps)return 0; else return x<0?-1:1; } bool operator < (const Point &a,const Point &b){ return dcmp(a.x-b.x)<0||(dcmp(a.x-b.x)==0&&dcmp(a.y-b.y)<0); } bool operator == (const Point &a,const Point &b){ return dcmp(a.x-b.x)==0&&dcmp(a.y-b.y)==0; } 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; } Vector Rotate(Vector A,double rad){ return Vector(A.x*cos(rad)-A.y*sin(rad),A.x*sin(rad)+A.y*cos(rad)); } int tubao(Point *p,int n,Point *ch){ sort(p,p+n); //n=unique(p,p+n)-p; int m=0; 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--; return m; } void readp(Point &A){ scanf("%lf%lf",&A.x,&A.y); } bool onsegment(Point p,Point a1,Point a2){ if(p==a1||p==a2)return false; return dcmp(Cross(a1-p,a2-p))==0&&dcmp(Dot(a1-p,a2-p))<0; } bool segmentcross(Point a1,Point a2,Point b1,Point b2){ if(a1==b1||a1==b2||a2==b1||a2==b2)return true; double c1=Cross(a2-a1,b1-a1),c2=Cross(a2-a1,b2-a1), c3=Cross(b2-b1,a1-b1),c4=Cross(b2-b1,a2-b1); return dcmp(c1)*dcmp(c2)<0&&dcmp(c3)*dcmp(c4)<0; } int intubao(Point *ch,int n,Point p){ Vector A,B; int flag=0; for(int i=0;i<n;i++){ A=ch[(i+1)%n]-ch[i]; B=p-ch[i]; if(onsegment(p,ch[i],ch[(i+1)%n])){ flag=-1; break; } else if(Cross(A,B)>0){ flag++; } } if(flag==-1||flag==n)return 1; return 0; } int T,n,m; Point p1[10005],ch1[10005],p2[10005],ch2[10005]; int main(){ while(~scanf("%d%d",&n,&m)&&!(n==0&&m==0)){ for(int i=0;i<n;i++){ readp(p1[i]); } for(int i=0;i<m;i++){ readp(p2[i]); } int m1=tubao(p1,n,ch1); int m2=tubao(p2,m,ch2); int flag=0; for(int i=0;i<m1;i++){ for(int j=0;j<m2;j++){ if(segmentcross(ch1[i],ch1[(i+1)%m1],ch2[j],ch2[(j+1)%m2])){ flag=1; break; } } } for(int i=0;i<m1;i++){ if(intubao(ch2,m2,ch1[i])){ flag=1; break; } } for(int i=0;i<m2;i++){ if(intubao(ch1,m1,ch2[i])){ flag=1; break; } } if(flag)printf("No\n"); else printf("Yes\n"); } }