This is an interactive problem. There is a robot in a hidden grid, and you are trying to get it from its starting cell to the target cell in this grid.

The grid is of size m x n, and each cell in the grid is either empty or blocked. It is guaranteed that the starting cell and the target cell are different, and neither of them is blocked.

Each cell has a cost that you need to pay each time you move to the cell. The starting cell's cost is not applied before the robot moves.

You want to find the minimum total cost to move the robot to the target cell. However, you do not know the grid's dimensions, the starting cell, nor the target cell. You are only allowed to ask queries to the GridMaster object.

The GridMaster class has the following functions:

  • boolean canMove(char direction) Returns true if the robot can move in that direction. Otherwise, it returns false.
  • int move(char direction) Moves the robot in that direction and returns the cost of moving to that cell. If this move would move the robot to a blocked cell or off the grid, the move will be ignored, the robot will remain in the same position, and the function will return -1.
  • boolean isTarget() Returns true if the robot is currently on the target cell. Otherwise, it returns false.

Note that direction in the above functions should be a character from {'U','D','L','R'}, representing the directions up, down, left, and right, respectively.

Return the minimum total cost to get the robot from its initial starting cell to the target cell. If there is no valid path between the cells, return -1.

Input & Output

Example 1 — Basic Grid Navigation
$ Input: grid = [[1,3,1],[1,0,1],[4,2,1]], start = [2,0], target = [2,2]
Output: 6
💡 Note: Robot starts at (2,0) with cost 4. One optimal path: move right to (2,1) with cost 2, then right to (2,2) with cost 1. Total: 4+2+1 = 7, but the starting cell cost isn't counted, so 2+1 = 3. Wait, let me recalculate: starting cost not applied, so path cost is 2+1 = 3.
Example 2 — Blocked Path
$ Input: grid = [[0,0,0],[1,1,1],[0,0,1]], start = [1,0], target = [1,2]
Output: -1
💡 Note: The middle row has a blocked cell at (1,1), making it impossible to reach target (1,2) from start (1,0).
Example 3 — Alternative Path
$ Input: grid = [[1,1,1,1],[2,2,2,2],[1,1,1,1]], start = [0,0], target = [2,3]
Output: 8
💡 Note: Robot can go down-right-right-right or right-right-right-down. Both paths have the same cost when optimal route is chosen.

Constraints

  • 1 ≤ m, n ≤ 100
  • grid[i][j] ≥ 1 indicates the cost to move to cell (i, j)
  • grid[i][j] = 0 indicates that cell (i, j) is blocked
  • The starting and target cells are guaranteed to be different and not blocked

Visualization

Tap to expand
Interactive Grid Exploration and PathfindingHidden Grid? ? ?? R ?? ? ?Robot starts hereDFS Exploration1 3 25 R 14 2 TGrid discovered!Dijkstra Path1→3→25 R→14→2→TMin cost: 5Key Challenge: Must explore unknown environment while optimizing path costInteractive API: canMove() • move() • isTarget()
Understanding the Visualization
1
Hidden Grid
Robot starts without knowing grid layout or target location
2
Exploration
Use DFS to discover reachable cells and their costs
3
Optimal Path
Apply Dijkstra's algorithm to find minimum cost route
Key Takeaway
🎯 Key Insight: Combine DFS exploration with Dijkstra's algorithm to handle the unknown grid efficiently
Asked in
Google 25 Facebook 18 Amazon 12 Microsoft 8
23.4K Views
Medium Frequency
~35 min Avg. Time
847 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