Fork me on GitHub

POJ 3608 Bridge Across Islands --凸包间距离,旋转卡壳

题意: 给你两个凸包,求其最短距离。

解法: POJ 我真的是弄不懂了,也不说一声点就是按顺时针给出的,不用调整点顺序。 还是说数据水了,没出乱给点或给逆时针点的数据呢。。我直接默认顺时针给的点居然A了,但是我把给的点求个逆时针凸包,然后再反转一下时针顺序,又WA了。这其中不知道有什么玄机。。

求凸包最短距离还是用旋转卡壳的方法,这里采用的是网上给出的一种方法:

英文版:        http://cgm.cs.mcgill.ca/~orm/mind2p.html

中文翻译版:  http://www.cnblogs.com/bless/archive/2008/08/06/1262438.html

输入的两个凸包须是顺时针。

分别以一个为主卡另外一个,两次取最小值即可。

算法就不分析了, 画个图理解一下就知道了。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#define pi acos(-1.0)
#define eps 1e-8
using namespace std;

struct Point{
    double x,y;
    Point(double x=0, double y=0):x(x),y(y) {}
    void input() { scanf("%lf%lf",&x,&y); }
};
typedef Point Vector;
int dcmp(double x) {
    if(x < -eps) return -1;
    if(x > eps) return 1;
    return 0;
}
template <class T> T sqr(T x) { return x * x;}
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 p) { return Vector(A.x*p, A.y*p); }
Vector operator / (Vector A, double p) { return Vector(A.x/p, A.y/p); }
bool operator < (const Point& a, const Point& b) { return a.x < b.x || (a.x == b.x && a.y < b.y); }
bool operator >= (const Point& a, const Point& b) { return a.x >= b.x && a.y >= b.y; }
bool operator <= (const Point& a, const Point& b) { return a.x <= b.x && a.y <= b.y; }
bool operator == (const Point& a, const Point& b) { return dcmp(a.x-b.x) == 0 && dcmp(a.y-b.y) == 0; }
double Dot(Vector A, Vector B) { return A.x*B.x + A.y*B.y; }
double Length(Vector A) { return sqrt(Dot(A, A)); }
double Angle(Vector A, Vector B) { return acos(Dot(A, B) / Length(A) / Length(B)); }
double Cross(Vector A, Vector B) { return A.x*B.y - A.y*B.x; }
Vector VectorUnit(Vector x){ return x / Length(x);}
Vector Normal(Vector x) { return Point(-x.y, x.x) / Length(x);}
double angle(Vector v) { return atan2(v.y, v.x); }

double DistanceToSeg(Point P, Point A, Point B) {
    if(A == B) return Length(P-A);
    Vector v1 = B-A, v2 = P-A, v3 = P-B;
    if(dcmp(Dot(v1, v2)) < 0) return Length(v2);
    if(dcmp(Dot(v1, v3)) > 0) return Length(v3);
    return fabs(Cross(v1, v2)) / Length(v1);
}
double SegDistancetoSeg(Point A,Point B,Point C,Point D) {
    return min(min(DistanceToSeg(C,A,B),DistanceToSeg(D,A,B)),min(DistanceToSeg(A,C,D),DistanceToSeg(B,C,D)));
}
Point DisP(Point A,Point B) { return Length(B-A); }

double MinDisOfTwoConvexHull(Point P[],int n,Point Q[],int m) {
    int Pymin = 0, Qymax = 0, i,j;
    for(i=0;i<n;i++) if(dcmp(P[i].y-P[Pymin].y) < 0) Pymin = i;
    for(i=0;i<m;i++) if(dcmp(Q[i].y-Q[Qymax].y) > 0) Qymax = i;
    P[n] = P[0], Q[m] = Q[0];
    double Mindis = 1e90, Tmp;
    for(i=0;i<n;i++) {
        while(dcmp(Tmp = Cross(P[Pymin+1]-P[Pymin],Q[Qymax+1]-P[Pymin])-Cross(P[Pymin+1]-P[Pymin],Q[Qymax]-P[Pymin])) > 0)
            Qymax = (Qymax+1)%m;
        if(dcmp(Tmp) < 0) Mindis = min(Mindis,DistanceToSeg(Q[Qymax],P[Pymin],P[Pymin+1]));
        else              Mindis = min(Mindis,SegDistancetoSeg(P[Pymin],P[Pymin+1],Q[Qymax],Q[Qymax+1]));
        Pymin = (Pymin+1)%n;
    }
    return Mindis;
}

Point P[10005],nP[10005],Q[10005],nQ[10005];

int main()
{
    int n,m,i;
    while(scanf("%d%d",&n,&m)!=EOF && n+m)
    {
        for(i=0;i<n;i++) P[i].input();
        for(i=0;i<m;i++) Q[i].input();
        printf("%.5f\n",min(MinDisOfTwoConvexHull(P,n,Q,m),MinDisOfTwoConvexHull(Q,m,P,n)));
    }
    return 0;
}
View Code

 

求凸包,反转,WA。。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#define pi acos(-1.0)
#define eps 1e-8
using namespace std;

struct Point{
    double x,y;
    Point(double x=0, double y=0):x(x),y(y) {}
    void input() { scanf("%lf%lf",&x,&y); }
};
typedef Point Vector;
int dcmp(double x) {
    if(x < -eps) return -1;
    if(x > eps) return 1;
    return 0;
}
template <class T> T sqr(T x) { return x * x;}
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 p) { return Vector(A.x*p, A.y*p); }
Vector operator / (Vector A, double p) { return Vector(A.x/p, A.y/p); }
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 a.x >= b.x && a.y >= b.y; }
bool operator <= (const Point& a, const Point& b) { return a.x <= b.x && a.y <= b.y; }
bool operator == (const Point& a, const Point& b) { return dcmp(a.x-b.x) == 0 && dcmp(a.y-b.y) == 0; }
double Dot(Vector A, Vector B) { return A.x*B.x + A.y*B.y; }
double Length(Vector A) { return sqrt(Dot(A, A)); }
double Angle(Vector A, Vector B) { return acos(Dot(A, B) / Length(A) / Length(B)); }
double Cross(Vector A, Vector B) { return A.x*B.y - A.y*B.x; }
Vector VectorUnit(Vector x){ return x / Length(x);}
Vector Normal(Vector x) { return Point(-x.y, x.x) / Length(x);}
double angle(Vector v) { return atan2(v.y, v.x); }

double DistanceToSeg(Point P, Point A, Point B) {
    if(A == B) return Length(P-A);
    Vector v1 = B-A, v2 = P-A, v3 = P-B;
    if(dcmp(Dot(v1, v2)) < 0) return Length(v2);
    if(dcmp(Dot(v1, v3)) > 0) return Length(v3);
    return fabs(Cross(v1, v2)) / Length(v1);
}
double SegDistancetoSeg(Point A,Point B,Point C,Point D) {
    return min(min(DistanceToSeg(C,A,B),DistanceToSeg(D,A,B)),min(DistanceToSeg(A,C,D),DistanceToSeg(B,C,D)));
}
Point DisP(Point A,Point B) { return Length(B-A); }
bool SegmentIntersection(Point A,Point B,Point C,Point D) {
    return max(A.x,B.x) >= min(C.x,D.x) &&
           max(C.x,D.x) >= min(A.x,B.x) &&
           max(A.y,B.y) >= min(C.y,D.y) &&
           max(C.y,D.y) >= min(A.y,B.y) &&
           dcmp(Cross(C-A,B-A)*Cross(D-A,B-A)) <= 0 &&
           dcmp(Cross(A-C,D-C)*Cross(B-C,D-C)) <= 0;
}
void SegIntersectionPoint(Point& P,Point a,Point b,Point c,Point d) {  //需保证ab,cd相交
    P.x = (Cross(d-a,b-a)*c.x - Cross(c-a,b-a)*d.x)/(Cross(d-a,b-a)-Cross(c-a,b-a));
    P.y = (Cross(d-a,b-a)*c.y - Cross(c-a,b-a)*d.y)/(Cross(d-a,b-a)-Cross(c-a,b-a));
}
void CounterClockwiseToClockWise(Point* p,Point *np,int n){
    np[0] = p[0];
    for(int i=1;i<n;i++) np[i] = p[n-i];
}
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 && dcmp(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 && dcmp(Cross(ch[m-1]-ch[m-2], p[i]-ch[m-2])) <= 0) m--;
        ch[m++] = p[i];
    }
    if(n > 1) m--;
    return m;
}
double MinDisOfTwoConvexHull(Point* P,int n,Point* Q,int m) {
    int Pymin = 0, Qymax = 0, i,j;
    for(i=0;i<n;i++) if(dcmp(P[i].y-P[Pymin].y) < 0) Pymin = i;
    for(i=0;i<m;i++) if(dcmp(Q[i].y-Q[Qymax].y) > 0) Qymax = i;
    P[n] = P[0], Q[m] = Q[0];
    double Mindis = 1e90, Tmp;
    for(i=0;i<n;i++) {
        while(dcmp(Tmp = Cross(P[Pymin+1]-P[Pymin],Q[Qymax+1]-P[Pymin])-Cross(P[Pymin+1]-P[Pymin],Q[Qymax]-P[Pymin])) > 0)
            Qymax = (Qymax+1)%m;
        if(dcmp(Tmp) < 0) Mindis = min(Mindis,DistanceToSeg(Q[Qymax],P[Pymin],P[Pymin+1]));
        else              Mindis = min(Mindis,SegDistancetoSeg(P[Pymin],P[Pymin+1],Q[Qymax],Q[Qymax+1]));
        Pymin = (Pymin+1)%n;
    }
    return Mindis;
}

Point P[10005],nP[10005],Q[10005],nQ[10005];

int main()
{
    int n,m,i;
    while(scanf("%d%d",&n,&m)!=EOF && n+m)
    {
        for(i=0;i<n;i++) P[i].input();
        for(i=0;i<m;i++) Q[i].input();
        ConvexHull(P,n,nP);
        CounterClockwiseToClockWise(nP,P,n);
        ConvexHull(Q,m,nQ);
        CounterClockwiseToClockWise(nQ,Q,m);
        printf("%.5f\n",min(MinDisOfTwoConvexHull(P,n,Q,m),MinDisOfTwoConvexHull(Q,m,P,n)));
    }
    return 0;
}
View Code

 

posted @ 2014-12-16 01:05  whatbeg  阅读(300)  评论(0编辑  收藏  举报