Twisted Mirror Path Count Problem
Description
LeetCode Problem 3665.
Given an m x n binary grid grid where:
grid[i][j] == 0represents an empty cell, andgrid[i][j] == 1represents a mirror.
A robot starts at the top-left corner of the grid (0, 0) and wants to reach the bottom-right corner (m - 1, n - 1). It can move only right or down. If the robot attempts to move into a mirror cell, it is reflected before entering that cell:
- If it tries to move right into a mirror, it is turned down and moved into the cell directly below the mirror.
- If it tries to move down into a mirror, it is turned right and moved into the cell directly to the right of the mirror.
If this reflection would cause the robot to move outside the grid boundaries, the path is considered invalid and should not be counted.
Return the number of unique valid paths from (0, 0) to (m - 1, n - 1).
Since the answer may be very large, return it modulo 10^9 + 7.
Note: If a reflection moves the robot into a mirror cell, the robot is immediately reflected again based on the direction it used to enter that mirror: if it entered while moving right, it will be turned down; if it entered while moving down, it will be turned right. This process will continue until either the last cell is reached, the robot moves out of bounds or the robot moves to a non-mirror cell.
Example 1:
Input: grid = [[0,1,0],[0,0,1],[1,0,0]]
Output: 5
Explanation:
Number Full Path
1 (0, 0) → (0, 1) [M] → (1, 1) → (1, 2) [M] → (2, 2)
2 (0, 0) → (0, 1) [M] → (1, 1) → (2, 1) → (2, 2)
3 (0, 0) → (1, 0) → (1, 1) → (1, 2) [M] → (2, 2)
4 (0, 0) → (1, 0) → (1, 1) → (2, 1) → (2, 2)
5 (0, 0) → (1, 0) → (2, 0) [M] → (2, 1) → (2, 2)
- [M] indicates the robot attempted to enter a mirror cell and instead reflected.
Example 2:
Input: grid = [[0,0],[0,0]]
Output: 2
Explanation:
Number Full Path
1 (0, 0) → (0, 1) → (1, 1)
2 (0, 0) → (1, 0) → (1, 1)
Example 3:
Input: grid = [[0,1,1],[1,1,0]]
Output: 1
Explanation:
Number Full Path
1 (0, 0) → (0, 1) [M] → (1, 1) [M] → (1, 2)
(0, 0) → (1, 0) [M] → (1, 1) [M] → (2, 1) goes out of bounds, so it is invalid.
Constraints:
m == grid.lengthn == grid[i].length2 <= m, n <= 500grid[i][j]is either0or1.grid[0][0] == grid[m - 1][n - 1] == 0
Solution
Approach: Dynamic Programming
For each cell (i, j) keep two counts: f[i][j][0], the number of ways to leave the cell to the right, and f[i][j][1], the number of ways to leave it downward (all modulo 10^9 + 7).
Let r = f[i][j - 1][0] be the number of paths that enter (i, j) moving right (0 if j = 0) and d = f[i - 1][j][1] the number that enter moving down (0 if i = 0). At the start cell there is one way to be there, so d = 1 and r = 0.
- Empty cell: a path continues in the direction it came from or turns, so both outgoing counts are
(r + d) mod (10^9 + 7). - Mirror: a path entering from above is turned right and a path entering from the left is turned down, so
f[i][j][0] = dandf[i][j][1] = r.
A reflection that would leave the grid has no later cell that reads it, so it is dropped automatically. The corners are empty cells, so the answer is f[m - 1][n - 1][0]. Cells are processed row by row, left to right.
Complexity: time O(mn), space O(mn).
class Solution:
def uniquePaths(self, grid: List[List[int]]) -> int:
mod = 10**9 + 7
m, n = len(grid), len(grid[0])
f = [[[0, 0] for _ in range(n)] for _ in range(m)]
for i, row in enumerate(grid):
for j, x in enumerate(row):
r = f[i][j - 1][0] if j else 0
d = f[i - 1][j][1] if i else 0
if i == 0 and j == 0:
d = 1
if x == 1:
f[i][j] = [d, r]
else:
s = (r + d) % mod
f[i][j] = [s, s]
return f[-1][-1][0]