难度中等487一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为“Start” )。
机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角(在下图中标记为“Finish”)。
问总共有多少条不同的路径?
int uniquePaths(int m, int n) {
if(m==1 || n ==1){
return 1;
}
vector<vector<int>> dp(m,vector<int>(n));
for(size_t i =0;i<m;++i){
for(size_t j =0;j<n;++j){
if(i ==0 || j == 0){
dp[i][j] = 1;
}
}
}
for(size_t i =1;i < m;++i){
for(size_t j =1;j <n;++j){
dp[i][j] = dp[i-1][j]+dp[i][j-1];
}
}
return dp[m-1][n-1];
}