Sub topic 6
Handling Blocked Cells
This chapter assumes you have read "Counting Unique Paths" and know the plain path-counting recurrence, dp[i][j] = dp[i-1][j] + dp[i][j-1]. It also assumes you can read nested Python loops over a 2D list.
Real grids often contain cells you cannot enter. Examples include walls in a maze, rocks on a map, or 1 values in an input matrix. Obstacles leave the recurrence unchanged. They add one rule on top of it: a blocked cell has zero ways to be reached.
The Rule: Blocked Means Zero
The state is the same as before. dp[i][j] is the number of right/down paths from the start to cell (i, j). If (i, j) is blocked, no path can end there, so dp[i][j] = 0. That zero then flows forward automatically. A neighbor that reads a blocked cell as its "top" or "left" source adds nothing from it.
The Implementation
def unique_paths_with_obstacles(grid):
m, n = len(grid), len(grid[0])
dp = [[0] * n for _ in range(m)]
for i in range(m):
for j in range(n):
if grid[i][j] == 1:
dp[i][j] = 0
elif i == 0 and j == 0:
dp[i][j] = 1
else:
top = dp[i - 1][j] if i > 0 else 0
left = dp[i][j - 1] if j > 0 else 0
dp[i][j] = top + left
return dp[m - 1][n - 1]
print(unique_paths_with_obstacles([[0, 0, 0], [0, 1, 0], [0, 0, 0]])) # 2The obstacle check comes first, so a blocked start cell gets 0 instead of 1. That zero then spreads through the whole table. A blocked goal is also handled, because the answer is read from a cell that was set to 0.
For the example grid with its center blocked, the filled table is:
| row | col 0 | col 1 | col 2 |
|---|---|---|---|
| 0 | 1 | 1 | 1 |
| 1 | 1 | 0 | 1 |
| 2 | 1 | 1 | 2 |
Common Errors
The most frequent bug comes from a shortcut in the obstacle-free version: setting the whole first row and first column to 1 before the main loop. With obstacles, that is wrong. Every cell past a blocked cell on the top edge is unreachable, because the only way to reach it is from the left.
def buggy(grid):
m, n = len(grid), len(grid[0])
dp = [[1] * n for _ in range(m)] # first row and column all set to 1
for i in range(1, m):
for j in range(1, n):
dp[i][j] = 0 if grid[i][j] == 1 else dp[i - 1][j] + dp[i][j - 1]
return dp[m - 1][n - 1]
print(buggy([[0, 1, 0]])) # 1 (wrong)
print(unique_paths_with_obstacles([[0, 1, 0]])) # 0 (correct)Other mistakes to watch for:
- Testing
dp[i][j] == 1when you meantgrid[i][j] == 1. One is the count table and the other is the input grid. - Skipping the check on the start cell. That returns a nonzero count when the start is a wall.
- Using a different obstacle encoding than the input. Some problems mark walls with
'#'orTrue.
Adding the obstacle check leaves the cost at O(m·n) time and O(m·n) space. Each cell still does a constant amount of work.
Exercise
What does uniquepathswith_obstacles return for [[0, 0], [1, 0]], and why?
Show answer
It returns 1. Cell (1, 0) is blocked, so it contributes 0. The goal (1, 1) gets top = dp[0][1] = 1 plus left = 0, so the only path is right, then down.
Exercise
The grid is [[0, 0, 0], [0, 0, 0], [0, 0, 1]]. What is the answer, and which line of code produces it?
Show answer
The answer is 0. The goal cell is blocked, so the first branch sets dp[2][2] = 0, and that is the value returned.