Shortest Path in a Hidden Grid - Problem
Imagine you're controlling a robot in a mysterious maze where you can't see the full layout! ๐ค
This is an interactive problem where you must navigate a robot through a hidden grid to reach a target cell. The twist? You have no idea what the grid looks like, where you started, or where the target is!
Your Mission:
- Guide the robot from its starting position to the target cell
- Find the shortest path possible
- Use only the limited API provided by the
GridMasterobject
Available API Methods:
canMove(direction)- Check if robot can move in direction ('U', 'D', 'L', 'R')move(direction)- Move robot (ignored if blocked/invalid)isTarget()- Check if current cell is the target
The grid contains empty cells (passable) and blocked cells (impassable). You need to explore the unknown terrain, map it out, and then find the optimal route!
Return: The minimum distance to the target, or -1 if unreachable.
Input & Output
example_1.py โ Basic Grid
$
Input:
grid = [[1,2],[-1,1]]
Robot starts at (1,0), Target at (0,1)
โบ
Output:
2
๐ก Note:
The robot starts at position (1,0) and needs to reach target at (0,1). The shortest path is: (1,0) โ (1,1) โ (0,1), which has length 2.
example_2.py โ Blocked Path
$
Input:
grid = [[1,1,1,1],[-1,0,0,2],[1,1,1,1]]
Robot starts at (1,0), Target at (1,3)
โบ
Output:
-1
๐ก Note:
The robot cannot reach the target because there's a wall of blocked cells (0) separating the starting position from the target. No valid path exists.
example_3.py โ Same Position
$
Input:
grid = [[2,-1]]
Robot starts at (0,1), Target at (0,0)
โบ
Output:
1
๐ก Note:
The robot starts adjacent to the target. One move to the left reaches the target, so the shortest distance is 1.
Visualization
Tap to expand
Understanding the Visualization
1
Start Exploration
Robot begins DFS exploration from unknown starting position, systematically checking all directions
2
Map Discovery
As robot explores, it builds a mental map of passable cells, blocked walls, and records target location when found
3
Complete Mapping
DFS continues until all reachable areas are explored, with robot backtracking to ensure complete coverage
4
Optimal Pathfinding
With complete map available, BFS finds shortest path from start to target on the discovered grid
Key Takeaway
๐ฏ Key Insight: In interactive problems with hidden information, separate the information gathering phase (exploration) from the problem-solving phase (pathfinding) for optimal results.
Time & Space Complexity
Time Complexity
O(m ร n)
DFS explores each cell once O(mn), BFS also visits each cell once O(mn)
โ Linear Growth
Space Complexity
O(m ร n)
Store the grid map and BFS queue/visited set
โก Linearithmic Space
Constraints
- 1 โค m, n โค 500 (grid dimensions)
- The robot can move in 4 directions: up, down, left, right
- The starting cell and target cell are guaranteed to be different
- Neither starting cell nor target cell is blocked
- Interactive problem - you cannot see the grid directly
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code