题目:
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?
思路:典型的二维动态规划,sum[i][j]表示当前(i,j)位置的路径之和
则有:sum[i][j]=sum[i-1][j]+sum[i][j-1]
二维动态规划的思路,是先对第一行,第一列列初始化,然后逐行动态规划。
class Solution {//sum[i][j]=sum[i-1][j]+sum[i][j-1]
public:
int uniquePaths(int m, int n) {
if(m<=0||n<=0) return 0;
vector<vector<int>> sum(m,vector<int>(n,1));
for(int i=1; i<m; i++){
for(int j=1; j<n;j++){
sum[i][j]=sum[i-1][j]+sum[i][j-1];
}
}
return sum[m-1][n-1];
}
};