ACM 1392 Surround the Trees

http://www.cnblogs.com/jbelial/archive/2011/08/05/2128624.html
典型的凸包问题

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
typedef struct 
{
        double x , y ;
}POINT ;
POINT result[110] ;// 模拟堆栈S,保存凸包上的点
POINT tree[110] ;
int n , top ;
double Distance ( POINT p1 , POINT p2 ) 
{
       return sqrt( (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 ( const void *p1 , const void *p2 )
{
    POINT *p3,*p4;
    double m;
    p3 = (POINT *)p1; 
    p4 = (POINT *)p2; 
    m = Multiply(tree[0] , *p3 , *p4) ;
    if(m < 0) return 1;
    else if(m == 0 && (Distance(tree[0] , *p3) < Distance(tree[0],*p4)))
        return 1;
    else return -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 len , 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 ;
          }
          else if ( 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;
                  }
                  else if(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();
          len = 0.0;
          for(int i = 0 ; i < top ; i ++)
               len = len + Distance(result[i] , result[i+1]) ;
          printf("%.2lf\n",len);

    }
    return 0 ;
}
#include<cstdio>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;

struct tree
{
    double x,y;
}P[105],s[105];
int cp(tree a,tree b)       //极角排序用的叉乘
{
    double r=a.x*b.y-a.y*b.x;
    if(r>0)
    {
        return 1;
    }
    else if(r==0)
    {
        return 2;
    }
    else
    {
        return 0;
    }
}
int cp_(tree a,tree b,tree c)       //寻找凸包点用的叉乘
{
    double r=(b.x-a.x)*(c.y-a.y)-(c.x-a.x)*(b.y-a.y);
    if(r>0)
    {
        return 1;
    }
    else if(r==0)
    {
        return 2;
    }
    else
    {
        return 0;
    }
}
double dis(tree a,tree b)
{
    double r=pow(a.x-b.x,2)+pow(a.y-b.y,2);
    r=sqrt(r);
    return r;
}
int main()
{
    int i,j,z,N,n,p1,a;
    double miny,d;
    while(scanf("%d",&N)&&N)
    {
        a=0;
        if(N==1)
        {
            scanf("%lf%lf",&s[0].x,&s[0].y);
            d=0;
            printf("%.2lf\n",d);
            continue;
        }
        if(N==2)
        {
            scanf("%lf%lf",&s[0].x,&s[0].y);
            scanf("%lf%lf",&s[1].x,&s[1].y);
            d=dis(s[0],s[1]);
            printf("%.2lf\n",d);
            continue;
        }
        scanf("%lf%lf",&s[0].x,&s[0].y);
        miny=s[0].y;
        p1=0;
        for(i=1;i<N;i++)
        {
            scanf("%lf%lf",&s[i].x,&s[i].y);
            if(miny>s[i].y)
            {
                miny=s[i].y;
                p1=i;
            }
        }
        if(p1!=0)
        {
            swap(s[p1],s[0]);
        }
        double x0=s[0].x;
        double y0=s[0].y;
        for(i=0;i<N;i++)
        {
            s[i].x-=x0;
            s[i].y-=y0;     //把最低点作为基点等效生成一个坐标系
        }
        for(i=2;i<N;i++)    //极角排序
        {
            z=cp(s[i-1],s[i]);
            if(z==2)
            {
                if(abs(s[i].x)<abs(s[i-1].x))
                {
                    swap(s[i],s[i-1]);      //如果是一条直线则从靠近基点的点开始,方便计数
                }
                continue;
            }
            j=i;
            while(z==0)
            {
                swap(s[j],s[j-1]);
                j--;
                z=cp(s[j-1],s[j]);
            }
        }
        P[0]=s[N]=s[0];
        P[1]=s[1];
        n=2;
        for(i=2;i<=N;i++)       //寻找凸包点
        {
            z=cp_(P[n-2],P[n-1],s[i]);
            if(z==1)
            {
                P[n]=s[i];
                n++;
                continue;
            }
            if(z==2)
            {
                P[n]=s[i];
                n++;
                a++;        //记录直线的次数
                continue;
            }
            while(z==0)
            {
                n--;
                z=cp_(P[n-2],P[n-1],s[i]);
                if(z==1||z==2)
                {
                    P[n]=s[i];
                    n++;
                }
            }
        }
        P[n]=P[0];
        d=0;
        for(i=0;i<n;i++)
        {
            d+=dis(P[i],P[i+1]);
        }
        if(a==N-1)
        {
            printf("%.2lf\n",d/2);
            continue;
        }
        printf("%.2lf\n",d);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值