Count Lattice Paths Through Forbidden Checkpoints
Math & Bit ManipulationAuto-graded · 17 test casesLast updated
Tests combinatorial counting of grid paths using inclusion-exclusion and binomial coefficients, requiring precise arithmetic on a bounded lattice.
Count Lattice Paths Through Forbidden Checkpoints
You are navigating an m × n grid from the top-left corner (0, 0) to the bottom-right corner (m-1, n-1). At each step you may move right or down only.
A set of cells is marked as forbidden — you must not step on any of them. Count the number of distinct paths from (0, 0) to (m-1, n-1) that avoid every forbidden cell, modulo 10^9 + 7.
The forbidden list may be empty. If (0, 0) or (m-1, n-1) is forbidden, or if no valid path exists, return 0.
Constraints
1 ≤ m, n ≤ 5000 ≤ len(forbidden) ≤ min(m * n, 2000)- Each element of
forbiddenis a list[r, c]with0 ≤ r < mand0 ≤ c < n. - Duplicate entries in
forbiddenare possible; treat them as a single blocked cell. - The grid may be a single cell (
m = n = 1) with no forbidden cells — in that case there is exactly 1 path (standing still).
Examples
Example 1
Input: count_lattice_paths(3, 3, [])
Output: 6
Example 2
Input: count_lattice_paths(3, 3, [[1, 1]])
Output: 2
Example 3
Input: count_lattice_paths(1, 1, [])
Output: 1
Hints
Hint 1
Build a 2-D DP table where dp[r][c] is the number of valid paths reaching cell (r, c). Initialize dp[0][0] = 1, mark forbidden cells as 0, and fill row by row.
Hint 2
When a cell is forbidden, force its DP value to 0 so it contributes nothing to cells that follow it.
Hint 3
For large grids the table can reach 500×500 = 250,000 cells; a straightforward O(m·n) DP pass fills this well within time limits. Remember to take every addition modulo 10^9 + 7.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Key Insight
Any path from (0, 0) to (m-1, n-1) using only right/down moves visits cells in strictly increasing order of r + c. This means a cell (r, c) can only be reached from the cell directly above it (r-1, c) or directly to its left (r, c-1). This optimal substructure makes dynamic programming the natural fit.
Algorithm
- Build a forbidden set from the input list (handles duplicates automatically via a Python
set). - Guard the endpoints: if either
(0, 0)or(m-1, n-1)is forbidden, immediately return0. - Fill the DP table row by row, left to right:
dp[0][0] = 1(the starting cell counts as one "path" to itself).- For every other cell: if it is forbidden,
dp[r][c] = 0; otherwise,dp[r][c] = (dp[r-1][c] + dp[r][c-1]) % MOD, treating out-of-bounds as0.
- Return
dp[m-1][n-1].
Why It Is Correct
The recurrence correctly sums all valid incoming paths. Forcing forbidden cells to 0 ensures they neither contribute to nor receive path counts — any path that would pass through a forbidden cell yields 0 when it multiplies through the table.
Complexity
| Complexity | |
|---|---|
| Time | O(m · n) |
| Space | O(m · n) for the DP table (reducible to O(n) with a rolling array, but not needed here) |
For m = n = 500 this is 250 000 operations — well within limits.
Reference Solution
from typing import List
def count_lattice_paths(m: int, n: int, forbidden: List[List[int]]) -> int:
MOD = 10**9 + 7
blocked = set()
for r, c in forbidden:
blocked.add((r, c))
# If start or end is blocked, answer is 0
if (0, 0) in blocked or (m - 1, n - 1) in blocked:
return 0
# dp[r][c] = number of valid paths reaching (r, c)
dp = [[0] * n for _ in range(m)]
dp[0][0] = 1
for r in range(m):
for c in range(n):
if r == 0 and c == 0:
continue
if (r, c) in blocked:
dp[r][c] = 0
continue
from_top = dp[r - 1][c] if r > 0 else 0
from_left = dp[r][c - 1] if c > 0 else 0
dp[r][c] = (from_top + from_left) % MOD
return dp[m - 1][n - 1]