Check if There is a Valid Path in a Grid - Problem
Imagine navigating through a city where each block has specific street connections that you must follow! You are given an m x n grid where each cell represents a street with predetermined connections:
- 1: Horizontal street (left ↔ right)
- 2: Vertical street (up ↔ down)
- 3: L-shaped street (left ↔ down)
- 4: J-shaped street (right ↔ down)
- 5: Γ-shaped street (left ↔ up)
- 6: r-shaped street (right ↔ up)
Your mission is to determine if there's a valid path from the top-left corner (0,0) to the bottom-right corner (m-1, n-1). You can only move between adjacent cells if their street connections are compatible - meaning one cell's connection direction must align with the neighboring cell's connection direction.
Goal: Return true if a valid path exists, false otherwise.
Input & Output
example_1.py — Basic Valid Path
$
Input:
grid = [[2,4,3],[6,5,2]]
›
Output:
true
💡 Note:
Starting at (0,0) with street type 2 (vertical), we can go down to (1,0) which has street type 6 (connects up and right). From there we can go right to (1,1) with type 5 (connects left and up), then up to (0,1) with type 4 (connects right and down), then right to (0,2) with type 3 (connects left and down), and finally down to (1,2) - our destination!
example_2.py — No Valid Path
$
Input:
grid = [[1,2,1],[1,2,1]]
›
Output:
false
💡 Note:
Starting at (0,0) with street type 1 (horizontal), we can only move horizontally. But the cell to the right (0,1) has street type 2 (vertical), which cannot connect to a horizontal street. There's no valid path to reach the bottom-right corner.
example_3.py — Single Cell Grid
$
Input:
grid = [[1,1,2]]
›
Output:
false
💡 Note:
We start at (0,0) and need to reach (0,2). From (0,0) with type 1, we can go right to (0,1) also with type 1. But from (0,1) we need to reach (0,2) with type 2 (vertical). Since type 1 only allows horizontal movement and type 2 only allows vertical movement, they cannot connect.
Visualization
Tap to expand
Understanding the Visualization
1
Map the Streets
Each cell has a specific street pattern: horizontal (1), vertical (2), or various L-shaped patterns (3-6)
2
Check Connections
Before moving to an adjacent cell, verify that both cells' street patterns allow the connection
3
Explore Systematically
Use DFS or BFS to explore all valid paths from start to destination
4
Track Visited Cells
Maintain a visited set to avoid cycles and infinite loops
Key Takeaway
🎯 Key Insight: Success depends on systematically checking connection compatibility between adjacent cells while using efficient graph traversal (DFS/BFS) to explore all possible paths.
Time & Space Complexity
Time Complexity
O(m*n)
Each cell is visited at most once, so linear in grid size
✓ Linear Growth
Space Complexity
O(m*n)
Space for queue and visited array
⚡ Linearithmic Space
Constraints
-
m == grid.length -
n == grid[i].length -
1 ≤ m, n ≤ 300 -
1 ≤ grid[i][j] ≤ 6 - Grid cells represent street types 1-6 with specific connection patterns
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code