POJ 3608 두 볼록 가방 의 최소 거리 구하 기 (볼록 가방 + 회전 케이스)

제목 링크:http://poj.org/problem?id=3608
방법: 볼록 팩 + 회전 케이스
이 문 제 를 영원히 잊 지 못 할 거 예요. WA + TLE 30 한번 징 그 러 워 요.
마지막 으로 G + + 제출% lf 오류% f 정 답 
#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
#define N 10000
const double eps = 1e-10;
//   
struct Point
{
	double x, y;
	Point(double x = 0, double y = 0) : x(x), y(y) {}
};
typedef Point Vector; //Vector   Point   

Point p[N+10],q[N+10],t[N+10];
int np, nq;
int dcmp(double x)
{
	if(fabs(x) < eps) return 0;
	else return x < 0 ? -1 : 1;
}

// - =  
Vector operator - (Point A, Point B) {return Vector(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;
}
//  :              XaXb + YaYb
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));}

//  :   v w     v w               XaYb - XbYa
double Cross(Vector A, Vector B){return A.x*B.y - A.y*B.x;}
double Area2(Point A, Point B, Point C){return Cross(B-A, C-A);}

bool cmp ( Point a, Point b )
{
    if ( a.x != b.x ) return a.x < b.x;
    else return a.y < b.y;
}
int ConvexHull(Point *p, int n, Point * ch)
{
	sort(p, p+n, cmp); //   x  ,   y  
	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 DistanceToSegment(Point P, Point A, Point B)
{
	if(A == B) return Length(P-A);
	Vector v1 = B - A, v2 = P - A, v3 = P - B;

	//P         
	if(dcmp(Dot(v1, v2)) < 0) return Length(v2);//P  A     
	else if(dcmp(Dot(v1, v3)) > 0) return Length(v3); //P  B     
	//P        
	else return fabs(Cross(v1, v2)) / Length(v1);
}

double Get_angle(Point a1, Point b1, Point a2, Point b2)
{
    Point t;
    t.x=a2.x-(b2.x-a1.x);
    t.y=a2.y-(b2.y-a1.y);
    return Cross(t-a1,b1-a1);
}
double slove()
{
    int yminP = 0, ymaxQ = 0;
    for(int i = 0; i < np; i++)
        if(p[i].y < p[yminP].y ||(p[i].y==p[yminP].y&&(p[i].x<p[yminP].x)))
            yminP = i;
    for(int i = 0; i < nq; i++)
        if(q[i].y > q[ymaxQ].y ||(q[i].y==q[ymaxQ].y&&(q[i].x>q[ymaxQ].x)))
            ymaxQ = i;

    p[np] = p[0];
    q[nq] = q[0];
    double ans = Length(p[yminP]-q[ymaxQ]);
    int rep = yminP, req = ymaxQ;
    do
    {
        double angle = Get_angle(p[yminP],p[yminP+1],q[ymaxQ],q[ymaxQ+1]);
        //double tmpp = Angle((p[((yminP-1)+np)%np]-p[yminP]),(p[yminP+1]-p[yminP]));
        //double tmpq = Angle((q[((ymaxQ-1)+nq)%nq]-q[ymaxQ]),(q[ymaxQ+1]-q[ymaxQ]));

        if(dcmp(angle) == 0)
        {
            ans = min(ans, DistanceToSegment(p[yminP],q[ymaxQ],q[ymaxQ+1]));
            ans = min(ans, DistanceToSegment(p[yminP+1],q[ymaxQ],q[ymaxQ+1]));
            ans = min(ans, DistanceToSegment(q[ymaxQ],p[yminP],p[yminP+1]));
            ans = min(ans, DistanceToSegment(q[ymaxQ+1],p[yminP],p[yminP+1]));
            yminP = (yminP+1)%np;
            ymaxQ = (ymaxQ+1)%nq;
        }
        else
        {

            if(dcmp(angle) < 0) 
            {
               ans = min(ans, DistanceToSegment(q[ymaxQ],p[yminP],p[yminP+1]));
               yminP = (yminP+1)%np;
            }
            else 
            {

                ans = min(ans, DistanceToSegment(p[yminP],q[ymaxQ],q[ymaxQ+1]));
                ymaxQ = (ymaxQ+1)%nq;
            }
        }
    }while(!(rep==yminP&&req==ymaxQ));

    return ans;
}

int main ()
{
    int n, m;
    while(~scanf("%d%d", &n, &m))
    {
        if(n == 0 && m == 0) break;
        for(int i = 0; i < n; i++)
            scanf("%lf %lf", &t[i].x, &t[i].y);
        np = ConvexHull(t,n,p);
        for(int i = 0; i < m; i++)
            scanf("%lf %lf", &t[i].x, &t[i].y);
        nq = ConvexHull(t,m,q);

        double ans = slove();
        printf("%.5f
",ans); } return 0; }

좋은 웹페이지 즐겨찾기