Count Paths Through a Grid with Blocked Cells
Dynamic ProgrammingAuto-graded · 17 test casesLast updated
Tests whether a candidate can apply bottom-up DP on a 2-D grid to count reachable paths from top-left to bottom-right while respecting blocked cells.
Count Paths Through a Grid with Blocked Cells
You are given a rectangular grid of rows rows and cols columns. Each cell is either open (0) or blocked (1). Starting from the top-left cell (0, 0), you want to reach the bottom-right cell (rows-1, cols-1) by moving only right or down at each step.
Write a function that returns the number of distinct paths from the top-left to the bottom-right corner.
- If the start cell
(0, 0)or the end cell(rows-1, cols-1)is blocked, return0. - A path is invalid if it passes through any blocked cell.
- If
rows == 0orcols == 0, return0.
The answer is guaranteed to fit in a 64-bit signed integer for all provided inputs.
Constraints
0 <= rows, cols <= 100- Each cell value is either
0(open) or1(blocked) gridis a list ofrowslists, each of lengthcols- Moving diagonally is not allowed; only right or down
Examples
Example 1
Input: count_grid_paths([[0, 0, 0], [0, 0, 0], [0, 0, 0]])
Output: 6
Example 2
Input: count_grid_paths([[0, 1, 0], [0, 0, 0], [0, 0, 0]])
Output: 3
Example 3
Input: count_grid_paths([[0, 0], [1, 0]])
Output: 1
Hints
Hint 1
Build a 2-D DP table where dp[r][c] is the number of ways to reach cell (r, c). Start from (0,0) and fill left-to-right, top-to-bottom.
Hint 2
For each open cell, dp[r][c] = dp[r-1][c] + dp[r][c-1]. For a blocked cell, dp[r][c] = 0 regardless of its neighbours.
Hint 3
Handle the first row and first column carefully: once a blocked cell is encountered, all cells beyond it in that row or column are also unreachable.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Intuition
We want to count every distinct sequence of right/down moves from the top-left corner to the bottom-right corner that avoids blocked cells. This has an optimal-substructure property: the number of ways to reach cell (r, c) equals the sum of ways to reach the cell directly above it (r-1, c) and the cell directly to its left (r, c-1), provided (r, c) is open.
Algorithm (Bottom-up DP)
- Edge cases: Return
0immediately if the grid is empty, or if either the start(0,0)or end(rows-1, cols-1)cell is blocked. - Initialise a
dptable of the same dimensions, all zeros. Setdp[0][0] = 1. - First column:
dp[r][0] = dp[r-1][0]ifgrid[r][0] == 0, else0. Once a blocked cell appears, every cell below it in that column inherits 0. - First row: Similarly,
dp[0][c] = dp[0][c-1]if open, else0. - Interior cells: For each
(r, c)withr >= 1, c >= 1:- If
grid[r][c] == 1,dp[r][c] = 0. - Otherwise
dp[r][c] = dp[r-1][c] + dp[r][c-1].
- If
- Answer is
dp[rows-1][cols-1].
Correctness
Because we only move right or down, any path to (r, c) must arrive from (r-1, c) or (r, c-1). The recurrence therefore enumerates all paths without double-counting, and a blocked cell correctly acts as a barrier by zeroing its DP entry (propagating 0 to any cell that can only be reached through it).
Complexity
- Time: O(rows × cols) — each cell is visited exactly once.
- Space: O(rows × cols) for the DP table (reducible to O(cols) with a rolling array, but the full table is clearer).
from typing import List
def count_grid_paths(grid: List[List[int]]) -> int:
if not grid or not grid[0]:
return 0
rows = len(grid)
cols = len(grid[0])
if grid[0][0] == 1 or grid[rows - 1][cols - 1] == 1:
return 0
dp = [[0] * cols for _ in range(rows)]
dp[0][0] = 1
for r in range(1, rows):
dp[r][0] = dp[r - 1][0] if grid[r][0] == 0 else 0
for c in range(1, cols):
dp[0][c] = dp[0][c - 1] if grid[0][c] == 0 else 0
for r in range(1, rows):
for c in range(1, cols):
if grid[r][c] == 1:
dp[r][c] = 0
else:
dp[r][c] = dp[r - 1][c] + dp[r][c - 1]
return dp[rows - 1][cols - 1]