620. Minimum Path Cost In a Hidden Grid
You are given a grid with an unknown structure and a GridMaster API with the following methods:
- boolean canMove(char direction) : Returns true if you can move in the given direction ('U', 'D', 'L', 'R'); otherwise, returns false.
- int move(char direction) : Moves in the given direction and returns the cost of the move. If the move is invalid, it returns -1.
- boolean isTarget() : Returns true if the current position is the target; otherwise, returns false.
Your task is to implement a function that uses this API to find the minimum path cost from the starting position to the target.
Example 1:
Input : grid = [[2,3],[1,1]], r1 = 0, c1 = 1, r2 = 1, c2 = 0
Output : 2
Explanation :
One possible interaction is described below:
The robot is initially standing on cell (0, 1), denoted by the 3.
- master.canMove('U') returns false.
- master.canMove('D') returns true.
- master.canMove('L') returns true.
- master.canMove('R') returns false.
- master.move('L') moves the robot to the cell (0, 0) and returns 2.
- master.isTarget() returns false.
- master.canMove('U') returns false.
- master.canMove('D') returns true.
- master.canMove('L') returns false.
- master.canMove('R') returns true.
- master.move('D') moves the robot to the cell (1, 0) and returns 1.
- master.isTarget() returns true.
- master.move('L') doesn't move the robot and returns -1.
- master.move('R') moves the robot to the cell (1, 1) and returns 1.
- We now know that the target is the cell (1, 0), and the minimum total cost to reach it is 2.
Example 2:
Input : grid = [[0,3,1],[3,4,2],[1,2,0]], r1 = 2, c1 = 0, r2 = 0, c2 = 2
Output : 9
Explanation :
The minimum cost path is (2,0) -> (2,1) -> (1,1) -> (1,2) -> (0,2).
Now Your Turn!
Pick the correct output for the given inputInput : grid = [[1,0],[0,1]], r1 = 0, c1 = 0, r2 = 1, c2 = 1
Still unsure what the problem is asking ?
Let’s go through a few more examples, step by step, to make it clearer.
Constraints:
- 1 <= n, m <= 100
- m == grid.length
- n == grid[i].length
- 0 <= grid[i][j] <= 100