首页 > 试题广场 >

求路径 ii

[编程题]求路径 ii
  • 热度指数:15990 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
给定一个m*n的地图,其中加入了一些障碍。每次只能向下或者向右走,问从左上角走到右下角有多少不同的路径?
分别用0和1代表空区域和障碍
例如
下图表示有一个障碍在3*3的图中央。
[
    [0,0,0],
    [0,1,0],
    [0,0,0]
]
有2条不同的路径
备注:m和n不超过100.

示例1

输入

[[0,1]]

输出

0
示例2

输入

[[1],[1]]

输出

0
头像 华科不平凡
发表于 2020-08-30 21:30:33
设dp[i][j]表示前i行和前j列最短路径数,状态方程如下: 当i >= 2 && j >= 2时, 如果obstacle[i-2][j-1] != 1, dp[i][j] += dp[i-1][j] 如果obstacle[i-1][j-2] != 1, dp[i][ 展开全文
头像 zhaungcheng
发表于 2021-06-07 20:09:51
class Solution { public: int uniquePathsWithObstacles(vector<vector<int> >& obstacleGrid) { // write code here if( 展开全文
头像 TAO201903261719260
发表于 2020-08-22 10:02:15
public class Solution { public int uniquePathsWithObstacles (int[][] obstacleGrid) { // write code here /其他地方与动态规划类似,有障碍的地方设为零就好/ 展开全文