zoukankan      html  css  js  c++  java
  • unique-paths

    /**
    *
    * @author gentleKay
    * A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).
    * The robot can only move either down or right at any point in time.
    * The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below).
    * How many possible unique paths are there?
    * Above is a 3 x 7 grid. How many possible unique paths are there?
    * Note: m and n will be at most 100.
    *
    *机器人位于M x N网格的左上角(下图中标记为“开始”)。
    *机器人只能在任何时间点向下或向右移动。
    *机器人正试图到达网格的右下角(在下图中标记为“完成”)。
    *有多少可能的唯一路径?
    *上面是一个3 x 7的网格。有多少可能的唯一路径?
    *注:M和N最多为100。
    */

    这是一题动态规划的题目,可以和这道题一起来看:

    https://www.cnblogs.com/strive-19970713/p/11271675.html

    /**
     * 
     * @author gentleKay
     * A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).
     * The robot can only move either down or right at any point in time. 
     * The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below).
     * How many possible unique paths are there?
     * Above is a 3 x 7 grid. How many possible unique paths are there?
     * Note: m and n will be at most 100.
     * 
     *机器人位于M x N网格的左上角(下图中标记为“开始”)。
     *机器人只能在任何时间点向下或向右移动。
     *机器人正试图到达网格的右下角(在下图中标记为“完成”)。
     *有多少可能的唯一路径?
     *上面是一个3 x 7的网格。有多少可能的唯一路径?
     *注:M和N最多为100。
     */
    
    public class Main18 {
    	public static void main(String[] args) {
    		System.out.println(Main18.uniquePaths(4, 6));
    	}
    	
    	public static int uniquePaths(int m, int n) {
            int [][]arr=new int[m][n];
    	    for(int i=0;i<n;i++){
    	        arr[0][i]=1;
    	    }for(int i=0;i<m;i++){
    	        arr[i][0]=1;
    	    }
    	    for(int i=1;i<m;i++){
    	        for(int j=1;j<n;j++){
    	            arr[i][j]=arr[i-1][j]+arr[i][j-1];
    	        }
    	    }
    	    return arr[m-1][n-1];
        }
    }
    

    分析:

      

      上面不只把 (3,7)的结果算出来了,而且还把(3,7)里面的结果也有显示。

  • 相关阅读:
    python装饰器的4种类型:函数装饰函数、函数装饰类、类装饰函数、类装饰类
    python中将函数赋值给变量时需要注意的一些问题
    python获得命令行输入的参数
    Python实现语音识别和语音合成
    pymysql
    Matplotlib
    级联,映射
    处理丢失数据
    Numpy,Pandas,Matplotlib
    crawlSpider,分布式爬虫,增量式爬虫
  • 原文地址:https://www.cnblogs.com/strive-19970713/p/11262614.html
Copyright © 2011-2022 走看看