Fork me on GitHub

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)里面的结果也有显示。

 

posted @ 2019-07-29 10:46  gentleKay  阅读(185)  评论(0编辑  收藏  举报