Unique Paths
来源:互联网 发布:ubuntu 网络映射 编辑:程序博客网 时间:2024/06/05 14:59
Description
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.
解题思路:题目要求算出从start到finish位置一共有多少种可能的走法,只能往右或往下走的限制带来了很大的方便,也就是说相对现在所处的位置,上一个位置在当前位置的左边或右边,因此用动态规划的方法就能很容易地解决问题,dp(i,j)表示从起点走到坐标为(i,j)的点的方法数,状态方程为dp(i,j)=dp(i-1,j)+dp(i,j-1),由于走到第一行中任意一个位置的走法都只能一种,就是一直往右,走到第一列中的任意一个位置也一样,一直往下,所以算出到达这些位置的走法以后,就能够一直递推下去了。程序代码如下:
class Solution {public: int uniquePaths(int m, int n) { vector<vector<int>> dp(m + 1, vector<int>(n + 1, 0)); for (int i = 1; i <= m; i++) { for (int j = 1; j <= n; j++) { if (i == 1 || j == 1) dp[i][j] = 1; else { dp[i][j] = dp[i - 1][j] + dp[i][j - 1]; } } } return dp[m][n]; }};
0 0
- Unique Paths
- Unique Paths
- Unique Paths
- Unique Paths
- Unique Paths
- Unique Paths
- Unique Paths
- Unique Paths
- Unique Paths
- Unique Paths
- Unique Paths
- Unique Paths
- Unique Paths
- Unique Paths
- Unique Paths
- Unique Paths
- Unique Paths
- Unique Paths
- mysql(一)加快ALTER TABLE操作的速度
- 常见对象-把字符串的首字母转大写其他转小写
- 面向对象设计
- 飞碟
- 第十一周:[leetCode] 153. Find Minimum in Rotated Sorted Array
- Unique Paths
- Java设计模式之单类模式
- 第二届CCCC天梯赛决赛总结
- W2.1 MySQL5.7 编译安装
- Android函数调用顺序库——AppMethodOrder使用介绍
- java微信公众号-第二天
- 读C++Primer的一点发现
- SDUT 3903 CF【Dp+排序】
- android集成二维码扫描