UniquePathsII

不同路径 II

题目介绍

不同路径 II

一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为 “Start” )。

机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角(在下图中标记为 “Finish” )。

现在考虑网格中有障碍物。那么从左上角到右下角将会有多少条不同的路径?

img

网格中的障碍物和空位置分别用 10 来表示。

示例1:

img

1
2
3
4
5
6
7
输入:obstacleGrid = [[0,0,0],[0,1,0],[0,0,0]]
输出:2
解释:
3x3 网格的正中间有一个障碍物。
从左上角到右下角一共有 2 条不同的路径:
1. 向右 -> 向右 -> 向下 -> 向下
2. 向下 -> 向下 -> 向右 -> 向右

示例2:

img

1
2
输入:obstacleGrid = [[0,1],[0,0]]
输出:1

提示:

  • m == obstacleGrid.length
  • n == obstacleGrid[i].length
  • 1 <= m, n <= 100
  • obstacleGrid[i][j] 为 0 或 1

题目解法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package algorithm;

public class UniquePathsII {

public static int uniquePathsWithObstacles(int[][] obstacleGrid) {
int m = obstacleGrid.length;
int n = obstacleGrid[0].length;

if (obstacleGrid[0][0] == 1 || obstacleGrid[m - 1][n - 1] == 1) {
return 0;
}

int[][] path = new int[m][n];
path[0][0] = 1;
for (int i = 1; i < m; i++) {
if (obstacleGrid[i][0] == 0) {
path[i][0] = path[i - 1][0];
} else {
path[i][0] = 0;
}
}
for (int i = 1; i < n; i++) {
if (obstacleGrid[0][i] == 0) {
path[0][i] = path[0][i - 1];
} else {
path[0][i] = 0;
}
}

for (int i = 1; i < m; i++) {
for (int j = 1; j < n; j++) {
if (obstacleGrid[i][j] == 1) {
path[i][j] = 0;
} else {
path[i][j] = path[i - 1][j] + path[i][j - 1];
}
}
}
return path[m - 1][n - 1];
}

public int uniquePathsWithObstacles1(int[][] obstacleGrid) {
int n = obstacleGrid.length, m = obstacleGrid[0].length;
int[] f = new int[m];

// 存储列,其实存储行也一样,默认的第一列后面的元素不计算,因为本身就是1,
// 因为有置0的存在,所以不必担心其中有障碍物的情况
f[0] = obstacleGrid[0][0] == 0 ? 1 : 0;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
if (obstacleGrid[i][j] == 1) {
f[j] = 0;
continue;
}
// 其实本格子就是左和上,上也就是上个f[j],左就是f[j-1],这个需要思考下,所以这是滚动数组
if (j - 1 >= 0 && obstacleGrid[i][j - 1] == 0) {
f[j] += f[j - 1];
}
}
}

return f[m - 1];
}

public static void main(String[] args) {
int[][] obstacleGrid = new int[][]{{0,0,0},{0,1,0},{0,0,0}};
System.out.println(uniquePathsWithObstacles(obstacleGrid));

obstacleGrid = new int[][]{{0,1},{0,0}};
System.out.println(uniquePathsWithObstacles(obstacleGrid));
}
}

打印:

1
2
2
1

思路:

代码给了两种解法,一种是基于上一题的延伸,一种是官方的解法,更加精炼,而且用到了滚动数组。


UniquePathsII
https://yangtzeshore.github.io/2021/04/17/UniquePathsII/
作者
Chen Peng
发布于
2021年4月17日
许可协议