题目地址:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1197
思路:
分离两种点,即为分离两种点组成的两个凸包:
1.任取A凸包中的一点,判断是否在B凸包中;任取B凸包中的一点,判断是否在A凸包中;
2.任取A凸包上一条线段,判断是否与B凸包中线段相交;任取B凸包上一条线段,判断是否与A凸包中线段相交。
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define debug
using namespace std;
struct Point
{
double x,y;
Point(double x=0.0,double y=0.0):x(x),y(y) {}
void read()
{
scanf("%lf%lf",&x,&y);
}
};
typedef Point Vector;
int dcmp(double x)
{
if(fabs(x)<0) return 0;
else return x<0?-1:1;
}
bool operator < (Point A,Point B)
{
if(A.x==B.x) return A.y<A.y;
else return A.x<B.x;
}
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 SegmentProperIntersection(Point a1,Point a2,Point b1,Point b2)
{
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;
}
bool isPointOnSegment(Point p,Point a1,Point a2)
{
return dcmp(Cross(a1-p,a2-p))==0&&dcmp(Dot(a1-p,a2-p))<0;
}
int ConvexHull(Point* p,int n,Point* ch)
{
sort(p,p+n);
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;
}
int isPointInPolygon(Point p,Point* poly,int n)
{
int wn=0;
for(int i=0; i<n; i++)
{
if(isPointOnSegment(p,poly[i],poly[(i+1)%n])) return -1;
int k=dcmp(Cross(poly[(i+1)%n]-poly[i],p-poly[i]));
int d1=dcmp(poly[i].y-p.y);
int d2=dcmp(poly[(i+1)%n].y-p.y);
if(k>0&&d1<=0&&d2>0) wn++;
if(k<0&&d2<=0&&d1>0) wn--;
}
if(wn!=0) return 1;
return 0;
}
const int maxn=500+50;
int m,c;
int num1,num2;
Point p1[maxn],p2[maxn];
Point convex1[maxn],convex2[maxn];
int checkIn()
{
for(int i=0; i<num1; i++)
{
if(isPointInPolygon(convex1[i],convex2,num2))
{
return 1;
}
}
for(int i=0; i<num2; i++)
{
if(isPointInPolygon(convex2[i],convex1,num1))
{
return 1;
}
}
return 0;
}
int checkIntersection()
{
for(int i=0; i<num1; i++)
{
for(int j=0; j<num2; j++)
{
if(SegmentProperIntersection(convex1[i],convex1[(i+1)%num1],convex2[j],convex2[(j+1)%num2]))
{
return 1;
}
}
}
return 0;
}
int main()
{
#ifdef debu
freopen("in.txt","r",stdin);
#endif // debug
while(scanf("%d%d",&m,&c)!=EOF&&(m+c))
{
for(int i=0; i<m; i++) p1[i].read();
for(int i=0; i<c; i++) p2[i].read();
num1=ConvexHull(p1,m,convex1);
num2=ConvexHull(p2,c,convex2);
if(!checkIn()&&!checkIntersection())
{
printf("Yes\n");
}
else
{
printf("No\n");
}
}
return 0;
}