A robot starts at the top-left cell (0, 0) of an m x n grid. At each step it moves one cell down, one cell left, or one cell right. It never moves up.
Some cells are blocked and can never be entered. The list blocked holds them: each element [r, c] marks the cell at row r and column c.
The robot may walk left and right within a row, but a path never visits the same cell twice. It can descend from a cell in row r to row r + 1 only when the cell directly below is open.
Count the distinct paths that end at a cell in the bottom row (row m - 1). The answer can be large, so return it modulo 109+7.
Function Signature
def count_robot_paths(m: int, n: int, blocked: list[list[int]]) -> int:
Rules
-
A path is the sequence of cells the robot visits, starting with
(0, 0)
and ending with the cell where it stops. That last cell must be in row
m - 1
.
-
Every step stays inside the grid and enters an open cell. No cell appears twice in a path.
-
The robot may stop at any cell it can reach in the bottom row, including the cell where it entered that row. When
m = 1
the start cell is already in the bottom row, so the path with no moves counts.
-
Two paths are different when their sequences of cells differ.
-
A cell listed in
blocked
more than once is simply blocked.
Constraints
-
1 <= m, n <= 100
-
0 <= len(blocked) <= m * n
-
len(blocked[i]) == 2
-
0 <= blocked[i][0] < m
(row index)
-
0 <= blocked[i][1] < n
(column index)
-
The start cell
(0, 0)
is never blocked.
Examples
Input: m = 3, n = 3, blocked = []
Output: 27
Nothing is blocked, so in every row the robot can walk to any of the 3 columns before it descends (or, in the bottom row, before it stops). Each row offers 3 choices no matter where the robot entered it, and 3 choices in each of 3 rows give 27 paths.
Input: m = 2, n = 3, blocked = [[0, 1]]
Output: 3
The blocked cell (0, 1) cuts column 0 off from column 2 in the top row, so the robot can only descend at column 0. From (1, 0) it can stop at column 0, 1 or 2 of the bottom row, which gives 3 paths.
Input: m = 3, n = 4, blocked = [[0, 2], [1, 0], [2, 1]]
Output: 4
In row 0 the robot can reach columns 0 and 1, and only column 1 has an open cell below. In row 1 it can walk from column 1 to column 1, 2 or 3; column 1 cannot descend because (2, 1) is blocked, so the robot enters row 2 at column 2 or 3, and from either entry it can stop at column 2 or 3, which gives 4 paths.