2016级算法第六次上机-F.AlvinZH的学霸养成记VI

1082 AlvinZH的学霸养成记VI

思路

难题,凸包。

分析问题,平面上给出两类点,问能否用一条直线将二者分离。

首先应该联想到这是一个凸包问题,分别计算两类点的凸包,如果存在符合题意的直线,那么这两个凸包(凸多边形)一定是不相交的。

计算凸包一般有两种方法,Graham扫描法和Jarvis步进法。

Graham扫描法比较简单,好理解,书中也有伪代码。先找到最左下点P0,对剩下的点相对P0进行极角排序。然后依次进栈判断。当算法终止时,栈中从底部到顶部,依次是按逆时针方向排列的凸包中的点(有时候需要利用这一点)。

这个方法有一个缺点是如果想求得纯粹的顶点,即不包含共线点,很容易出错。中间的点可以通过叉积直接排除,对栈底部和顶部还需要额外的判断。

bool cmp(const Point& p1,const Point& p2)
{
    double C = Cross(p1-p[0], p2-p[0]);
    return C ? C>0 : dis(p[0],p1) < dis(p[0],p2);
}
//点集凸包
void Graham(int n)
{
	double x=p[0].x;
	double y=p[0].y;
	int mi=0;
	for(int i=1;i<n;i++)//找到最左下点
	{
		if(p[i].x<x||(p[i].x==x&&p[i].y<y))
		{
			x=p[i].x;
			y=p[i].y;
			mi=i;
		}
	}

	Point tmp=p[mi];
	p[mi]=p[0];
	p[0]=tmp;

	sort(p+1,p+n,cmp);//极角排序

	stack[0]=p[0];
	stack[1]=p[1];
	stack[2]=p[2];
	int top=2;
	for (int i=3 ; i<n ; ++i)
	{
		while(crossProd(stack[top-1],stack[top],p[i])<=0&&top>=2)
			--top;
		stack[++top]=p[i];
	}
}

解题代码中采用Jarvis步进法,先把点集按先y后x排序,从最低点到最高点遍历,再从最高点到最低点遍历,即可找到凸包上所有的点。算法终止时,数组CH中也是按逆时针顺序排列的,且最后一点与第一点相同。

x、y意义上是相同的,下列代码中采用从左到右、再从右到左,排序也作相应改变,先x后y,效果一样。

bool operator < (const Point& p1, const Point& p2) {
    return p1.x < p2.x || (p1.x == p2.x && p1.y < p2.y);
}
//点集凸包
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;
}

第二个问题,判断红点和蓝点分别组成的两个凸包(凸多边形)是否相离。完全暴力判断,别无他法。

①任取一个红点,判断是否在蓝凸包内or上。如果是,则无解。蓝点红凸包同理。

二分法:判断点在凸多边形内,n个点总时间复杂度O(nlgn)。

②任取红凸包上的一条线段和蓝凸包上的一条线段,判断二者是否相交。如果相交(不一定是规范相交,有公共点就算相交),则无解。

方法:叉积的应用。如果另一线段两端点分别在这一线段的两侧,那么线段可能相交(也可能在线段外),否则不可能相交。对另一线段采用相同方法就可判断出是否相交了。

任何一个凸包退化成点或者线段时本来需要特判,上面两种情况已经包含。

分析

求解凸包的两种算法中,Graham扫描法时间复杂度(O(nlgn),Jarvis步进法时间复杂度为)O(nh)。

求解凸包方法:http://www.csie.ntnu.edu.tw/~u91029/ConvexHull.html(页面粉粉的

判断相离复杂度较高,分析无意义。

参考代码

#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
using namespace std;

//精度判断
const double eps = 1e-10;
double dcmp(double x) {
    if(fabs(x) < eps) return 0;
    else return x < 0 ? -1 : 1;
}

struct Point {
    double x, y;
    Point(double x=0, double y=0):x(x),y(y) {}
};
Point operator - (const Point& A, const Point& B) {
    return Point(A.x-B.x, A.y-B.y);
}
double Cross(const Point& A, const Point& B) {
    return A.x*B.y - A.y*B.x;
}
double Dot(const Point& A, const Point& B) {
    return A.x*B.x + A.y*B.y;
}
bool operator < (const Point& p1, const Point& p2) {
    return p1.x < p2.x || (p1.x == p2.x && p1.y < p2.y);
}
bool operator == (const Point& p1, const Point& p2) {
    return p1.x == p2.x && p1.y == p2.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),
            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;
}

//点集凸包,Jarvis步进法
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 IsPointInPolygon(const Point& p, const vector<Point>& poly) {
    int wn = 0;
    int n = poly.size();
    for(int i=0; i<n; ++i) {
        const Point& p1 = poly[i];
        const Point& 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);
        int d2 = dcmp(p2.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;//外部
}
bool ConvexPolygonDisjoint(const vector<Point> ch1, const vector<Point> ch2) {
    int c1 = ch1.size();
    int c2 = ch2.size();
    for(int i=0; i<c1; ++i)
        if(IsPointInPolygon(ch1[i], ch2) != 0) return false;
    for(int i=0; i<c2; ++i)
        if(IsPointInPolygon(ch2[i], ch1) != 0) 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()
{
    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
");
    }
}
原文地址:https://www.cnblogs.com/AlvinZH/p/8185363.html