Count Lattice Paths Avoiding Forbidden Cells
Math & Bit ManipulationAuto-graded · 18 test casesLast updated
Tests combinatorics by counting monotone lattice paths from (0,0) to (m,n) that avoid a given set of forbidden grid cells, using inclusion-exclusion over path prefixes.
Count Lattice Paths Avoiding Forbidden Cells
You are standing at cell (0, 0) on an (m+1) × (n+1) integer grid (columns 0..m, rows 0..n).
You want to reach cell (m, n) by taking only right steps (column + 1) or up steps (row + 1).
Some cells are forbidden — you may not step on any of them.
Return the number of valid monotone paths from (0, 0) to (m, n) that avoid every forbidden cell, modulo 10^9 + 7.
(0, 0)and(m, n)are guaranteed not to be forbidden.- A path visits exactly
m + n + 1cells (including start and end). - Any cell
(c, r)with0 ≤ c ≤ m,0 ≤ r ≤ nmay be forbidden.
Approach hint
The number of unrestricted monotone paths from (c1, r1) to (c2, r2) (with c2 ≥ c1, r2 ≥ r1) is the binomial coefficient C((c2-c1) + (r2-r1), c2-c1).
Use dynamic programming over the forbidden cells (sorted topologically along paths) combined with the above formula to subtract paths that pass through at least one forbidden cell.
Constraints
1 ≤ m, n ≤ 5000 ≤ len(forbidden) ≤ 200- Each forbidden cell is given as
[col, row]with0 ≤ col ≤ m,0 ≤ row ≤ n. - No duplicate forbidden cells.
(0, 0)and(m, n)are never forbidden.- Return the answer modulo
10^9 + 7.
Examples
Example 1
Input: count_lattice_paths(3, 3, [])
Output: 20
Example 2
Input: count_lattice_paths(3, 3, [[1, 1]])
Output: 8
Example 3
Input: count_lattice_paths(2, 2, [[1, 0], [0, 1]])
Output: 0
Hints
Hint 1
Sort the forbidden cells so that cell A comes before cell B whenever a monotone path could visit A before B (i.e., A.col ≤ B.col and A.row ≤ B.row). A topological order by (col+row, col) works.
Hint 2
Let bad[i] = number of paths from (0,0) to forbidden[i] that avoid all forbidden cells with smaller index. Use the formula: bad[i] = C(ci+ri, ci) - sum over all j < i reachable-from-j-to-i of bad[j] * C((ci-cj)+(ri-rj), ci-cj).
Hint 3
The final answer is C(m+n, m) minus the sum of bad[i] * C((m-ci)+(n-ri), m-ci) over all forbidden cells i, all computed modulo 10^9+7.
Worked solution
Hard Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Key Insight
Every monotone path from (0,0) to (m,n) takes exactly m right steps and n up steps, so the total unrestricted count is C(m+n, m).
To count paths that avoid all forbidden cells, we use a standard inclusion-exclusion / DP on forbidden cells technique:
-
Topological order: Sort forbidden cells by
(col+row, col). Cell A can appear on a monotone path before cell B only ifA.col ≤ B.colandA.row ≤ B.row; the sumcol+rowstrictly increases along any monotone path, so this ordering is consistent. -
DP definition: Let
bad[i]= number of monotone paths from(0,0)tocells[i]that do not pass through any earlier forbidden cell. Compute:bad[i] = C(ci+ri, ci) - sum_{j < i, cj≤ci, rj≤ri} bad[j] * C((ci-cj)+(ri-rj), ci-cj)The first term counts all paths to
cells[i]; the subtracted terms remove paths that hit some earlier forbidden cellcells[j]for the first time. -
Final answer:
answer = C(m+n, m) - sum_i bad[i] * C((m-ci)+(n-ri), m-ci)Each term removes paths that touch forbidden cell
i(for the first time, counted bybad[i]) and then continue to(m,n). -
Binomial coefficients are computed with precomputed factorials and modular inverse factorials using Fermat's little theorem (
inv_fact[N] = fact[N]^(MOD-2) mod MOD).
Correctness
The DP exactly implements inclusion-exclusion: bad[i] is the count of "first-bad-cell-is-i" paths, and the final subtraction removes every path that hits at least one forbidden cell exactly once (the first time).
Complexity
- Precomputation: O(m+n) for factorials.
- DP: O(k²) where k ≤ 200 is the number of forbidden cells.
- Overall: O(m + n + k²) time, O(m + n + k) space — well within limits.
from typing import List
def count_lattice_paths(m: int, n: int, forbidden: List[List[int]]) -> int:
MOD = 10**9 + 7
MAX = m + n + 1
fact = [1] * (MAX + 1)
for i in range(1, MAX + 1):
fact[i] = fact[i - 1] * i % MOD
inv_fact = [1] * (MAX + 1)
inv_fact[MAX] = pow(fact[MAX], MOD - 2, MOD)
for i in range(MAX - 1, -1, -1):
inv_fact[i] = inv_fact[i + 1] * (i + 1) % MOD
def C(total, choose):
if choose < 0 or choose > total or total < 0:
return 0
return fact[total] * inv_fact[choose] % MOD * inv_fact[total - choose] % MOD
def paths(c1, r1, c2, r2):
if c2 < c1 or r2 < r1:
return 0
return C((c2 - c1) + (r2 - r1), c2 - c1)
cells = sorted(forbidden, key=lambda x: (x[0] + x[1], x[0]))
k = len(cells)
bad = [0] * k
for i in range(k):
ci, ri = cells[i]
bad[i] = paths(0, 0, ci, ri)
for j in range(i):
cj, rj = cells[j]
if cj <= ci and rj <= ri:
bad[i] = (bad[i] - bad[j] * paths(cj, rj, ci, ri)) % MOD
ans = paths(0, 0, m, n)
for i in range(k):
ci, ri = cells[i]
ans = (ans - bad[i] * paths(ci, ri, m, n)) % MOD
return ans % MOD