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.

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

Visualization

Tap to expand
Shortest Path in a Hidden Grid INPUT Hidden Grid (Unknown to Robot) 1 (0,0) 2 TARGET -1 WALL 1 START R Legend: Robot Start (1,0) Target (0,1) Wall (blocked) GridMaster API: canMove(dir) - check movement move(dir) - move robot isTarget() - check if at goal dir: U, D, L, R ALGORITHM STEPS (DFS) 1 Explore with DFS Use API to discover grid Mark visited cells 2 Build Virtual Grid Store discovered cells Record target location 3 Backtrack Movement Move back after exploring Try all 4 directions 4 BFS for Shortest Path Run BFS on virtual grid Find min distance to target Exploration Path: 1,0 0,0 0,1 TARGET! FINAL RESULT Shortest Path Found: (0,0) TARGET (0,1) WALL START (1,0) Step 1 Output: 2 OK - Path Found! Distance = 2 moves Path: (1,0) --> (0,0) --> (0,1) Move UP, then RIGHT Key Insight: Since the grid is hidden, we must first EXPLORE using DFS to build a virtual map of all reachable cells. During DFS, backtrack after each move to explore all directions. Once the grid is mapped and target found, use BFS on the virtual grid to find the shortest path. Return -1 if target is unreachable. TutorialsPoint - Shortest Path in a Hidden Grid | DFS Exploration + BFS Shortest Path
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