HDU-1348 Wall 凸包

来源:互联网 发布:linux c 读写ini文件 编辑:程序博客网 时间:2024/04/30 05:37



#include<stdio.h>#include<algorithm>#include<math.h>using namespace std;const int maxn = 1005;#define PI 3.14159265int n,r;struct Point{double x,y;Point( double x = 0,double y = 0 ): x(x),y(y) { }}point[maxn],ch[maxn];typedef Point Vector;bool cmp( Point A,Point B ){    //return A.x < B.x || ( A.x == B.x && A.y < B.y );    if( A.x == B.x )        return A.y < B.y;    return A.x < B.x;}double Cross( Vector A,Vector B ){return A.x * B.y - A.y * B.x;}Vector operator - ( Vector A,Vector B ){return Vector( A.x - B.x,A.y - B.y );}double getDis( Point A,Point B ){    return sqrt( (A.x - B.x)*(A.x - B.x) + ( A.y - B.y )*( A.y - B.y ) );}int ConvexHull(){    sort( point,point+n,cmp );int m = 0;for( int i = 0; i < n; i ++ ){while( m > 1 && Cross( ch[m-1]-ch[m-2],point[i]-ch[m-2] ) <= 0 )m --;ch[m++] = point[i];}int k = m;for( int i = n-2; i >= 0; i -- ){while( m > k && Cross( ch[m-1]-ch[m-2],point[i]-ch[m-2] ) <= 0 )m --;ch[m++] = point[i];}if( n > 1 )m --;return m;}int main(){int C;scanf("%d",&C);while( C -- ){scanf("%d%d",&n,&r);for( int i = 0; i < n; i ++ )scanf("%lf%lf",&point[i].x,&point[i].y);        int m = ConvexHull();        double dis = 0;        for( int i = 1; i < m; i ++ )            dis += getDis( ch[i],ch[i-1] );        dis += getDis( ch[m-1],ch[0] );        dis += 2 * PI * r;        printf("%.0lf\n",dis);        if( C )            puts("");}return 0;}


0 0