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: In this interactive problem, the robot explores the grid using DFS to discover all reachable cells and their costs. Once exploration is complete, Dijkstra's algorithm finds the minimum cost path from start to target. The starting cell's cost is not included in the final path cost.
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 robot explores the grid but discovers that the target cell is unreachable due to blocked cells (cost 0). The algorithm returns -1 to indicate no valid path exists.
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: The robot explores a larger grid and finds multiple possible paths. Dijkstra's algorithm efficiently determines the path with minimum total cost by considering all discovered cells and their costs.

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
Minimum Path Cost in a Hidden Grid INPUT Hidden Grid (3x3) 1 3 1 1 X 1 4 START 2 1 TARGET Start [2,0] Target [2,2] Blocked (0) Cost value grid = [[1,3,1],[1,0,1],[4,2,1]] start=[2,0], target=[2,2] ALGORITHM STEPS 1 DFS Exploration Query GridMaster to explore all reachable cells from start 2 Build Local Grid Store costs and find target during exploration 3 Dijkstra's Algorithm Use min-heap to find shortest path to target 4 Return Min Cost Return distance to target or -1 if unreachable Priority Queue (Min-Heap) (0,0) (1,1) (2,2) Process: (cost, row, col) FINAL RESULT Minimum Cost Path Found 1 3 1 1 X 1 S 2 T Path Cost Breakdown: [2,0]-->[1,0]: +1 [1,0]-->[0,0]: +1 [0,0]-->[0,2]: +1 [0,2]-->[1,2]: +1 [1,2]-->[2,2]: +1 Output: 6 Key Insight: Since the grid is hidden, we must first explore it using DFS with GridMaster queries (canMove, move, isTarget). After building a local representation of reachable cells and their costs, apply Dijkstra's algorithm to find the minimum cost path. The blocked cell (cost=0) creates obstacles that force longer paths around it. TutorialsPoint - Minimum Path Cost in a Hidden Grid | DFS Exploration + Dijkstra's Algorithm
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