poj2187(凸包)

来源:互联网 发布:bt种子下载器for mac 编辑:程序博客网 时间:2024/06/05 04:20

有n个地方,找到两点之间的最大距离



其实我看着题目这个winner其实没必要任意两个点都去,在凸包中完全可以经过别的点那样就不用走那么长了

但是题意就是任意两个点


找出凸包,然后遍历所有的凸包点找最大距离


看别的讲解中有一个旋转卡壳算法


没看懂


#include<stdio.h>

#include<stdlib.h>

#include<math.h>

typedef struct

{

    double x , y ;

} POINT ;

POINT result[50005] ;//模拟堆栈S,保存凸包上的点

POINT tree[50005] ;

int n , top ;

double Distance ( POINT p1 ,POINT p2 )

{

    returnsqrt( (p1.x - p2.x)*(p1.x - p2.x) + (p1.y - p2.y)*(p1.y - p2.y) ) ;

}

int Distance2 ( POINT p1 ,POINT p2 )

{

    return  (p1.x - p2.x)*(p1.x - p2.x) + (p1.y - p2.y)*(p1.y - p2.y) ;

}

double Multiply(POINT p1 ,POINT p2 , POINT p3)// 叉积

{

    return ( (p2.x - p1.x)*(p3.y - p1.y) - (p2.y - p1.y)*(p3.x - p1.x) ) ;

}

int cmp ( constvoid *p1 , constvoid *p2 )

{

    POINT *p3,*p4;

    double m;

    p3 = (POINT *)p1;

    p4 = (POINT *)p2;

    m = Multiply(tree[0] , *p3 , *p4) ;

    if(m <0) return1;

    else if(m == 0 && (Distance(tree[0] , *p3) < Distance(tree[0],*p4)))

        return1;

    elsereturn -1;

}

void Tubao ()

{

    int i ;

    result[0].x =tree[0].x;

    result[0].y =tree[0].y;

    result[1].x =tree[1].x;

    result[1].y =tree[1].y;

    result[2].x =tree[2].x;

    result[2].y =tree[2].y;

    top =2;

    for ( i =3 ; i <= n ; ++ i )

    {

        while (Multiply(result[top - 1] , result[top] ,tree[i]) <= 0 )

            top -- ;                         //出栈

        result[top +1].x =tree[i].x ;

        result[top +1].y =tree[i].y ;

        top ++ ;

    }

}

int main ()

{

    int pos ;

    double  temp , px , py ;

    while (scanf ( "%d" , &n ) !=EOF , n )

    {

        py = -1 ;

        for (int i = 0 ; i <n ; ++ i )

        {

            scanf ("%lf%lf" , &tree[i].x , &tree[i].y ) ;

            

        }

        if (n == 1 )

        {

            printf ("0.00\n" ) ;

            continue ;

        }

        elseif ( n ==2 )

        {

            printf ("%.2lf\n" , Distance(tree[0] , tree[1]) ) ;

            continue ;

        }

        for (int i = 0 ; i <n ; ++ i )

        {

            if(py == -1 ||tree[i].y < py)

            {

                px = tree[i].x;

                py = tree[i].y;

                pos = i;

            }

            elseif(tree[i].y == py &&tree[i].x < px)

            {

                px = tree[i].x;

                py = tree[i].y;

                pos = i;

            }

        }

        temp = tree[0].x ;                     // 找出y最小的点

        tree[0].x =tree[pos].x ;

        tree[pos].x = temp ;

        temp = tree[0].y ;

        tree[0].y =tree[pos].y ;

        tree[pos].y = temp ;

        qsort(&tree[1],n - 1,sizeof(double) *2,cmp);

        tree[n].x =tree[0].x;

        tree[n].y =tree[0].y;

        Tubao();

        int len =0;

        for(int i =0 ; i < top ; i ++)

        {

            for(int j=i+1;j<top;j++)

            {

                int dis=Distance2(result[i] ,result[j]);

                if(len<dis)

                    len=dis;


            }

        }

        printf("%d\n",len);

        

        

    }

    return0 ;

}