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 GridMaster object

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
Robot Navigation: Exploration vs PathfindingPhase 1: DFS ExplorationRTSystematic explorationPhase 2: BFS Shortest PathRTOptimal path found๐ŸŽฏ Key InsightSeparate exploration from pathfindingDFS maps the unknown terrain, BFS finds optimal routeStart PositionTarget PositionEmpty CellBlocked Cell
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)

n
2n
โœ“ Linear Growth
Space Complexity
O(m ร— n)

Store the grid map and BFS queue/visited set

n
2n
โšก 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
Asked in
Google 42 Amazon 31 Meta 28 Microsoft 19
28.4K Views
Medium Frequency
~25 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen