Leetcode 63.不同路径II

63. Unique Paths II

题目

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

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

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

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

说明:m 和 n 的值均不超过 100。

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

方法

机器人只能向下或者向右移动,因此第一行的cell只能左边到达,第一列的cell只能从上方到达,其他任意的cell可以从左边或者上方到达。如果该cell有阻碍物,则对任何路径都没有贡献

方法1:动态规划 & not in place

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Solution(object):
def uniquePathsWithObstacles(self, obstacleGrid):
"""
:type obstacleGrid: List[List[int]]
:rtype: int
"""
if not obstacleGrid:
return
r,c=len(obstacleGrid),len(obstacleGrid[0])
dp=[[0 for _ in range(c)] for _ in range(r)]
dp[0][0]=1-obstacleGrid[0][0]

for i in range(1,r):
dp[i][0]=dp[i-1][0]*(1-obstacleGrid[i][0])
for i in range(1,c):
dp[0][i]=dp[0][i-1]*(1-obstacleGrid[0][i])
for i in range(1,r):
for j in range(1,c):
dp[i][j]=(dp[i-1][j]+dp[i][j-1])*(1-obstacleGrid[i][j])
return dp[-1][-1]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution(object):
def uniquePathsWithObstacles(self, obstacleGrid):
"""
:type obstacleGrid: List[List[int]]
:rtype: int
"""
if not obstacleGrid:
return 0
m,n=len(obstacleGrid),len(obstacleGrid[0])
dp=[[0 for _ in range(n)] for _ in range(m)]
dp[0][0]=1 if not obstacleGrid[0][0] else 0 # 确定(0,0)的状态
for i in range(1,m): # 确定 除(0,0)外,第一列的状态
dp[i][0]=1 if dp[i-1][0] and not obstacleGrid[i][0] else 0
for i in range(1,n): # 确定除(0,0)外,第一行的状态
dp[0][i]=1 if dp[0][i-1] and not obstacleGrid[0][i] else 0
for i in range(1,m):
for j in range(1,n):
if obstacleGrid[i][j]:
dp[i][j]=0
else:
dp[i][j]=dp[i-1][j]+dp[i][j-1]
return dp[-1][-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
class Solution(object):
def uniquePathsWithObstacles(self, obstacleGrid):
"""
:type obstacleGrid: List[List[int]]
:rtype: int
"""
m = len(obstacleGrid)
n = len(obstacleGrid[0])

# If the starting cell has an obstacle, then simply return as there would be
# no paths to the destination.
if obstacleGrid[0][0] == 1:
return 0

# Number of ways of reaching the starting cell = 1.
obstacleGrid[0][0] = 1

# Filling the values for the first column
for i in range(1,m):
obstacleGrid[i][0] = int(obstacleGrid[i][0] == 0 and obstacleGrid[i-1][0] == 1)

# Filling the values for the first row
for j in range(1, n):
obstacleGrid[0][j] = int(obstacleGrid[0][j] == 0 and obstacleGrid[0][j-1] == 1)

# Starting from cell(1,1) fill up the values
# No. of ways of reaching cell[i][j] = cell[i - 1][j] + cell[i][j - 1]
# i.e. From above and left.
for i in range(1,m):
for j in range(1,n):
if obstacleGrid[i][j] == 0:
obstacleGrid[i][j] = obstacleGrid[i-1][j] + obstacleGrid[i][j-1]
else:
obstacleGrid[i][j] = 0

# Return value stored in rightmost bottommost cell. That is the destination.
return obstacleGrid[m-1][n-1]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Solution(object):
def uniquePathsWithObstacles(self, obstacleGrid):
"""
:type obstacleGrid: List[List[int]]
:rtype: int
"""
r,c=len(obstacleGrid),len(obstacleGrid[0])
dp=[[0 for _ in range(c)] for _ in range(r)]

if obstacleGrid[0][0]==0:
dp[0][0]=1

for i in range(r):
for j in range(c):
if obstacleGrid[i][j]==0:
if i==j==0:
continue
else:
dp[i][j]=dp[i-1][j]+dp[i][j-1]
return dp[-1][-1]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution(object):
def uniquePathsWithObstacles(self, obstacleGrid):
"""
:type obstacleGrid: List[List[int]]
:rtype: int
"""
r,c=len(obstacleGrid),len(obstacleGrid[0])
dp=[[0 for _ in range(c)] for _ in range(r)]

if obstacleGrid[0][0]==0:
dp[0][0]=1

for i in range(r):
for j in range(c):
if obstacleGrid[i][j]==1:
dp[i][j]=0
else:
if i!=0:
dp[i][j]+=dp[i-1][j]
if j!=0:
dp[i][j]+=dp[i][j-1]
return dp[r-1][c-1]

方法2:动态规划 & in place

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution(object):
def uniquePathsWithObstacles(self, obstacleGrid):
"""
:type obstacleGrid: List[List[int]]
:rtype: int
"""
if not obstacleGrid:
return
r,c=len(obstacleGrid),len(obstacleGrid[0])
obstacleGrid[0][0]=1-obstacleGrid[0][0]

for i in range(1,r):
obstacleGrid[i][0]=obstacleGrid[i-1][0]*(1-obstacleGrid[i][0])
for i in range(1,c):
obstacleGrid[0][i]=obstacleGrid[0][i-1]*(1-obstacleGrid[0][i])
for i in range(1,r):
for j in range(1,c):
obstacleGrid[i][j]=(obstacleGrid[i-1][j]+obstacleGrid[i][j-1])*(1-obstacleGrid[i][j])
return obstacleGrid[-1][-1]

参考资料