zoukankan      html  css  js  c++  java
  • HDU_1030 Delta-wave 常数时间

    Delta-wave

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4768    Accepted Submission(s): 1811

    Problem Description
    A triangle field is numbered with successive integers in the way shown on the picture below.
    The traveller needs to go from the cell with number M to the cell with number N. The traveller is able to enter the cell through cell edges only, he can not travel from cell to cell through vertices. The number of edges the traveller passes makes the length of the traveller's route.
    Write the program to determine the length of the shortest route connecting cells with numbers N and M.
     
    Input
    Input contains two integer numbers M and N in the range from 1 to 1000000000 separated with space(s).
     
    Output
    Output should contain the length of the shortest route.
     
    Sample Input
    6 12
     
    Sample Output
    3
     
    Source
    #include <iostream>
    #include <cmath>
    using namespace std;
    
    struct Node{
        int n;              //数字
        int a;              //同层时从左到右的序数
        int h;              //深度
        Node ( int num ):
            n(num)
        {
            h = (int) sqrt( (double) n-1 ) + 1;
            a = num-(h-1)*(h-1);
        }
        Node (){}
    };
    
    int dfs ( Node& x , Node& y , int d ){     // d 指的是距离 
    
        if ( x.h  == y.h )
            return d+=abs ( x.n-y.n );
        else if ( x.a&1 )                    // 序数为奇数的x在下三角
        {
            x.h++;
            x.a++;
            x.n=(x.h-1)*(x.h-1)+x.a;
            dfs ( x , y , ++d );
        }
        else                          //上三角的情况
        {
            if ( y.a > x.a +( y.h - x.h ) )    // y如果在x的右边
            {
                x.h++;
                x.a+=2;
                x.n=(x.h-1)*(x.h-1)+x.a;
                d+=2;
                dfs ( x,y,d);
            }
            else
            {
                x.h++;
                x.n=(x.h-1)*(x.h-1)+x.a;
                d+=2;
                dfs ( x,y,d);
            }
        }
    }
    
    int main (){
    
        int n1,n2;
        while ( cin>>n1>>n2 )
        {
            Node x( n1 );
            Node y( n2 );
            if ( x.n > y.n )                 //只讨论y.n>=x.n的情况
            {
                Node temp;
                temp = x;
    
    
    
                x=y;
                y=temp;
            }
            cout<< dfs ( x , y , 0 ) <<endl;
        }
        return 0;
    }
    #include <iostream>
    #include <cmath>
    using namespace std;
    
    int caculate_x ( int n ){                                //从上到下数第x层
        return (int)sqrt( (double)n-1 ) + 1;                 //   x=(int)sqrt(n-1)+1
    }
    
    int caculate_y ( int n , int x ){                    //从左上到右下第y层
        return n- ((x-1)*(x-1))/2 +1;                       //第x层最左端的数为(x-1)^2+1 , 最右端的数为x^2
    }
    
    int caculate_z ( int n , int x ){                     //从右上到左下第z层
        return (x*x-n)/2 + 1;                                  
    }
    
    int main (){
    
        int m,n,dmin, n_x , n_y , n_z , m_x , m_y , m_z;
        while ( cin>>m>>n )
        {
            m_x = caculate_x ( m );                                   //分别计算出n,m的x,y,z
            m_y = caculate_y ( m , m_x );
            m_z = caculate_z ( m , m_x );
            n_x = caculate_x ( n );
            n_y = caculate_y ( n , n_x );
            n_z = caculate_z ( n , n_x );
            cout<<abs( m_z-n_z ) + abs( m_y+n_y ) + abs( m_x+n_x )<<endl;            //这个至今没搞懂...
        }
        return 0;
    }//这个不知道怎么没过,但是这个思想是过了的//我会说这是hdu的discuss里看到的?
     
  • 相关阅读:
    Linux环境下搭建Git仓库
    Linux环境下安装zookeeper
    nginx: [error] open() "/var/run/nginx/nginx.pid" failed (2: No such file or directory)
    Dart语言特性必备了解!
    Flutter中打造多行列列表GridView组件的使用
    Flutter常用组件(Widget)解析-Scaffold
    Flutter常用组件(Widget)解析-ListView
    Flutter常用组件(Widget)解析-Image
    Flutter常用组件(Widget)解析-Text
    Flutter常用组件(Widget)解析-Container
  • 原文地址:https://www.cnblogs.com/neverchanje/p/3476948.html
Copyright © 2011-2022 走看看