Problem: 1391. Check if There is a Valid Path in a Grid
Level: Medium
Check whether there is a valid path from the top-left cell to the bottom-right cell. Each grid cell has a street type, and you can only move to a neighboring cell if both streets connect to each other.
Solution:
- Use BFS starting from (0, 0).
- For each street type, store the two directions it can move.
- When moving to a neighbor, check whether the neighbor has a direction that connects back to the current cell.
- If yes, push it into the queue and mark it as visited. At the end, return whether (n - 1, m - 1) was visited.