You are given two integers m and n that describe a grid with m rows and n columns, and a list blocked_cols of forbidden column indices.
A robot starts on any cell of the top row (row 0) and wants to reach any cell of the bottom row (row m - 1). From cell (r, c) it may move to (r + 1, c - 1), (r + 1, c) or (r + 1, c + 1), as long as the destination is inside the grid. The robot may never occupy a cell whose column index appears in blocked_cols, and that includes the starting and ending cells.
Return the total number of distinct paths from any allowed top-row cell to any allowed bottom-row cell.
Function Signature
def count_paths(m: int, n: int, blocked_cols: list[int]) -> int:
...
Rules
-
A path is the sequence of cells it visits, one cell per row. Two paths are distinct when their sequences differ in at least one row, so paths with different starting cells are always distinct.
-
When
m = 1
, the starting cell is also the ending cell, so every allowed cell of the single row counts as one path.
-
blocked_cols
may list the same column more than once. A repeat has no extra effect.
-
Return the exact count. It grows exponentially with
m
and can have hundreds of digits, which a Python
int
holds without overflow.
Constraints
-
1 <= m, n <= 1000
-
0 <= len(blocked_cols) <= n
-
0 <= blocked_cols[i] < n
Examples
Input: m = 3, n = 4, blocked_cols = [1]
Output: 9
Column 1 is blocked in every row, so the allowed columns are 0, 2 and 3. A robot starting in column 0 can only stay in column 0, which gives 1 path. Columns 2 and 3 form a strip where each of the two moves has 2 choices, so each of those two starting cells gives 4 paths, 8 in total, and 1 + 8 = 9.
Input: m = 2, n = 3, blocked_cols = []
Output: 7
Nothing is blocked. From column 0 the robot can end in column 0 or 1 (2 paths), from column 1 in any of the three columns (3 paths), and from column 2 in column 1 or 2 (2 paths), so the total is 2 + 3 + 2 = 7.
Input: m = 1, n = 5, blocked_cols = [0, 4]
Output: 3
With a single row each allowed cell is both a start and an end. Columns 1, 2 and 3 are allowed, so there are 3 paths.