leetcode #62 in cpp

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?

Solution:

Suppose if w are currently in  a cell, we can only move right or down. Thus the number of paths from this cell to the destination is the sum the number of paths to destination from the cell right next to it,  and the number of paths to the destination from the cell blow it .

We use DP to solve this.

dp[i][j] = number of paths of the position(i+1, j + 1) to the destination

= dp[i+1][j] + d[[i][j+1]

The initialization involves the bottom row and rightmost column, both initialized to 1 since they can move in only 1 direction.

Code:

class Solution {
public:
int uniquePaths(int m, int n) {
vector<vector<int>> dp(m, vector<int>(n,0));//dp[i][j] = number of paths to destination
dp[m-1][n-1] = 1;//destination to 1
for(int i = 0; i < m && n-1 >=0; i ++){// right border to 1
dp[i][n-1] = 1;
}
for(int i = 0; i < n && m-1 >= 0; i ++){//bottom border to 1
dp[m-1][i] = 1;
}
for(int i = m-2; i >=0; i --){
for(int j = n-2; j>=0; j --){
dp[i][j]+= dp[i][j+1] + dp[i+1][j];//current path number = num of its righr cell paths + num of its down cell paths to destination
}
}
return dp[0][0];
}
};

• 本文已收录于以下专栏：

[LeetCode]62 不同的路径总数

Unique Paths(不同的路径总数)【难度：Medium】 A robot is located at the top-left corner of a m x n grid (marked ...
• qq_14821023
• 2016年02月28日 13:31
• 702

[LeetCode]63 不同的路径总数之二

Unique Paths II(不同的路径总数之二)【难度：Medium】 Follow up for “Unique Paths”:Now consider if some obstacles a...
• qq_14821023
• 2016年02月28日 13:42
• 355

【LeetCode】62. Unique Paths解法及注释

62. Unique Paths A robot is located at the top-left corner of a m x n grid (marked 'Start' in the ...
• Jin_Kwok
• 2016年04月08日 11:50
• 240

【leetcode算法】更新至62题

1、T104  树的深度 public class T104 {     }     class TreeNode {       int val;       TreeNode le...
• gol_phing
• 2015年07月07日 09:45
• 279

leetcode || 62、Unique Paths

problem: A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diag...
• hustyangju
• 2015年04月02日 11:24
• 634

(Java)LeetCode-62. Unique Paths

A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). ...
• u012848330
• 2016年10月06日 21:54
• 263

LeetCode 62

Unique Paths     机器人在最左上角，它只能向右和向下走，找出所有的独一无二的路径，使它能达到最右下角位置。     对已这个题我首先的思路是递归，因为机器人只能向下和向右走，所以将下和...
• zr1076311296
• 2016年06月08日 23:16
• 2037

LeetCode-62-Unique Paths(动态规划)-Medium

• eddy_liu
• 2016年03月14日 08:51
• 1100

LeetCode Solution(持续更新，java>c++)

• disappearedgod
• 2014年04月13日 21:52
• 8831

[leetcode] 【分治法】 50. Pow(x, n)

Implement pow(x, n). 题意 实现幂函数。 题解 使用分治法求解。 分：将n分成n/2  直到n=0时，返回1； 治：对n为偶数，返回两数相乘的结果，奇数再乘多一个x...
• u014654002
• 2016年06月21日 00:37
• 381

举报原因： 您举报文章：leetcode #62 in cpp 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)