Problem Description
Given an m x n grid where each cell is one of six types representing streets that connect two directions, determine if there is a valid path from the upper-left cell (0, 0) to the bottom-right cell (m - 1, n - 1). The path must follow the directions allowed by the type of each street, and movement between cells is only possible if both cells provide a road connection between them.
Key Insights
- Each cell type (from 1 to 6) defines specific allowed directions where a move is possible.
- The movement is only valid if the neighbor cell supports a connection back to the current cell.
- A mapping from cell type to allowed move directions simplifies determining reachable neighbors.
- Both Depth-First Search (DFS) and Breadth-First Search (BFS) can be used to explore the grid.
- Use a visited set (or equivalent) to avoid cycles and redundant work.
Space and Time Complexity
Time Complexity: O(m * n) - In the worst case, every cell is visited once. Space Complexity: O(m * n) - For the queue/stack and visited set used during traversal.
Solution
We can solve the problem using a BFS (or DFS) approach. First, define a mapping from each cell type to its allowed directions:
- Type 1: left and right
- Type 2: up and down
- Type 3: left and down
- Type 4: right and down
- Type 5: left and up
- Type 6: right and up
Also, define the opposites of these directions to ensure that when moving from cell A to B, cell B’s type must have the corresponding reverse direction to connect back to A.
Start from cell (0, 0) and explore allowed directions. For each move:
- Validate that the new cell is within bounds.
- Check if the neighbor cell's type allows a connection back in the opposite direction.
- If a valid connection exists, add the cell to the queue (or stack) for further exploration.
- Continue until reaching (m - 1, n - 1) or exhausting all possibilities.
Code Solutions
Below are code solutions in Python, JavaScript, C++, and Java with detailed comments.