unique-paths 怼烎@ 2021-11-09 08:36 292阅读 0赞 /\*\* \* \* @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。 \*/ ### 这是一题动态规划的题目,可以和这道题一起来看:[ ][Link 1] ### ### [https://www.cnblogs.com/strive-19970713/p/11271675.html][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]; } } 分析: ![1432431-20190729104246075-806499535.png][] #### 上面不只把 (3,7)的结果算出来了,而且还把(3,7)里面的结果也有显示。 #### 转载于:https://www.cnblogs.com/strive-19970713/p/11262614.html [Link 1]: https://www.cnblogs.com/strive-19970713/p/11262614.html [https_www.cnblogs.com_strive-19970713_p_11271675.html]: https://www.cnblogs.com/strive-19970713/p/11271675.html [1432431-20190729104246075-806499535.png]: /images/20211109/5ad41482308b423880b65681f76113e1.png
还没有评论,来说两句吧...