hdu1392-Surround the Trees

hdu1392- Surround the Trees

题链:http://acm.hdu.edu.cn/showproblem.php?pid=1392
就是求凸包周长,拿来练练手咯.
用的是Andrew算法,用Graham_scan算法也毛什么,都挺好的
注意事项在代码里了
#include 
   
   
    
    
#include 
    
    
     
     
#include 
     
     
      
      
#include 
      
      
       
       
#include 
       
       
         #include 
        
          #define maxn 105 using namespace std; const double eps = 1e-8,Pi=3.14159265; int n; inline int dcmp(double x)//三态函数 { if(fabs(x) < eps) return 0; else return x>0 ? 1 : -1; } #define Vector Point struct Point { double x,y; inline Point(double x=0,double y=0):x(x),y(y) {} }p[maxn],ch[maxn]; bool myCmp(Point A, Point B) { if(A.x != B.x) return A.x < B.x; else return A.y < B.y; } 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);} bool operator == (const Vector& A, const Vector& B) {return dcmp(A.x-B.x)==0 && dcmp(A.y-B.y)==0;} inline double Cross(Vector A, Vector B)//叉积 { return A.x * B.y - A.y * B.x; } int ConvexHull() { sort(p,p+n,myCmp); int m = 0 ; for(int i = 0; i 
         
           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 Dis(Point A, Point B) { return sqrt((double)(A.x - B.x)*(A.x - B.x) + (A.y - B.y)*(A.y - B.y)); } int main() { int i ; int a , b ; int len ; double result ; while( scanf("%d",&n) != EOF , n){ result = 0 ; for(int i = 0; i < n; i++){ cin>>a>>b; p[i] = Point(a,b); }///只一点要注意:只有三个点以上(包括三个)才能成环 if( n <= 1 ){ printf("0.00\n"); continue; } if ( n == 2 ){ printf( "%.2lf\n" , Dis( p[0] , p[1] ) ) ; continue ; } len = ConvexHull(); for( i = 1 ; i < len ; i++ ){ result += Dis( ch[i-1] , ch[i] ) ; } printf( "%.2lf\n" , result ) ; } return 0 ; } 
          
         
       
      
      
     
     
    
    
   
   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值