zoukankan      html  css  js  c++  java
  • 走N*M格 步数(即遍历二叉树)

    package huawei;
    
    /**
     * 请注意不要修改包名、类名,否则将导致考试成绩失效
     */
    public class JavaTest
    {
        /**
         * 计算出nxm个宫格从左上解走到右下角,总共有多少种走法,不允许走回头路,即: 只能往右走和往下走,不能往上和往左走。
         *
         * @param n : 横向的格子数;m: 竖向的格子数
         * @return :返回走法个数。
         */
        private static int column;
    
        private static int row;
        private static int count;
    
        public long getLatticePaths(int n, int m)
        {
            if(n<=0 || m<=0)
            {
                return -1;
            }
    
            count = 0;
            row = n;
            column = m;
            interator(1, 0);
    
            interator(0,1);
    
            return count;
        }
        public long interator(int n, int m)
        {
    
            if(n<column)
            {
                interator(n + 1, m);
            }
            if(m<row)
            {
                interator(n, m+1);
            }
    
            if(n==column && m == row)
            {
                count++;
            }
    
            return 0;
        }
    
    
    
        public static void main(String[] args)
        {
            JavaTest c = new JavaTest();
            int n = 2;
            int m = 2;
            System.out.println("the total paths is: " + c.getLatticePaths(n, m));
        }
    }
    

      单元测试:

    package testcase;
    
    import static org.junit.Assert.*;
    import org.junit.Test;
    import huawei.JavaTest;
    
    public class JavaTestTest extends JavaTest {
    
        @Test
        public void test_Case1() {
            assertEquals(getLatticePaths(2,2),6);
        }
    
        @Test
        public void test_Case2() {
            assertEquals(getLatticePaths(6,2),28);
        }
    }
    

      

  • 相关阅读:
    微信小程序
    js
    js
    uni
    uni/微信小程序
    uni/微信小程序
    ES6...扩展运算符(数组或类数组对象)
    微信小程序
    微信小程序
    玩转storm
  • 原文地址:https://www.cnblogs.com/tjw-nau/p/3375474.html
Copyright © 2011-2022 走看看