Check if There is a Valid Path in a Grid Problem
Description
LeetCode Problem 1391.
You are given an m x n grid. Each cell of grid represents a street. The street of grid[i][j] can be:
1which means a street connecting the left cell and the right cell.2which means a street connecting the upper cell and the lower cell.3which means a street connecting the left cell and the lower cell.4which means a street connecting the right cell and the lower cell.5which means a street connecting the left cell and the upper cell.6which means a street connecting the right cell and the upper cell.

You will initially start at the street of the upper-left cell (0, 0). A valid path in the grid is a path that starts from the upper left cell (0, 0) and ends at the bottom-right cell (m - 1, n - 1). The path should only follow the streets.
Notice that you are not allowed to change any street.
Return true if there is a valid path in the grid or false otherwise.
Example 1:

Input: grid = [[2,4,3],[6,5,2]]
Output: true
Explanation: As shown you can start at cell (0, 0) and visit all the cells of the grid to reach (m - 1, n - 1).
Example 2:

Input: grid = [[1,2,1],[1,2,1]]
Output: false
Explanation: As shown you the street at cell (0, 0) is not connected with any street of any other cell and you will get stuck at cell (0, 0)
Example 3:
Input: grid = [[1,1,2]]
Output: false
Explanation: You will get stuck at cell (0, 1) and you cannot reach cell (0, 2).
Constraints:
m == grid.lengthn == grid[i].length1 <= m, n <= 3001 <= grid[i][j] <= 6
Solution
Approach: Breadth-First Search Along the Streets
Each street type opens to two sides of its cell. Number the sides 0 up, 1 right, 2 down and 3 left; then type 1 opens {left, right}, 2 opens {up, down}, 3 opens {left, down}, 4 opens {right, down}, 5 opens {left, up} and 6 opens {right, up}.
Run a BFS from (0, 0). From a cell we may only leave through the two sides its street opens, and the step into the neighbor is allowed only if the neighbor's street opens back toward us, that is, it contains the opposite side (d + 2) % 4. This second check is the tricky part: in Example 2, cell (0, 0) is a horizontal street pointing right, but the cell to its right is a vertical street, so the two do not connect even though they touch.
Mark cells as seen when they enter the queue, and answer true as soon as (m - 1, n - 1) is reached. A 1 x 1 grid is already at its goal.
Because the connections are symmetric (a step from A to B is allowed exactly when the step from B to A is), the reachable cells form a connected component, so a union-find over all valid neighbor pairs works too. BFS is iterative, so it is safe even for a 300 x 300 grid whose only path snakes through every cell.
Complexity: O(m * n) time and space: each cell is visited at most once and has at most two exits.
from collections import deque
class Solution:
def hasValidPath(self, grid: List[List[int]]) -> bool:
m, n = len(grid), len(grid[0])
moves = [(-1, 0), (0, 1), (1, 0), (0, -1)] # 0 up, 1 right, 2 down, 3 left
# The two sides each street type opens to.
opens = [None, (3, 1), (0, 2), (3, 2), (1, 2), (3, 0), (1, 0)]
seen = [[False] * n for _ in range(m)]
seen[0][0] = True
queue = deque([(0, 0)])
while queue:
i, j = queue.popleft()
if i == m - 1 and j == n - 1:
return True
for d in opens[grid[i][j]]:
x, y = i + moves[d][0], j + moves[d][1]
# The neighbor must open back toward us (the opposite side, d + 2).
if 0 <= x < m and 0 <= y < n and not seen[x][y] and (d + 2) % 4 in opens[grid[x][y]]:
seen[x][y] = True
queue.append((x, y))
return False