LeetCode 1778. Shortest Path in a Hidden Grid
原题链接在这里:https://leetcode.com/problems/shortest-path-in-a-hidden-grid/description/
题目:
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.
You want to find the minimum distance 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.
Thr GridMaster
class has the following functions:
boolean canMove(char direction)
Returnstrue
if the robot can move in that direction. Otherwise, it returnsfalse
.void move(char direction)
Moves the robot in that direction. If this move would move the robot to a blocked cell or off the grid, the move will be ignored, and the robot will remain in the same position.boolean isTarget()
Returnstrue
if the robot is currently on the target cell. Otherwise, it returnsfalse
.
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 distance between the robot's initial starting cell and the target cell. If there is no valid path between the cells, return -1
.
Custom testing:
The test input is read as a 2D matrix grid
of size m x n
where:
grid[i][j] == -1
indicates that the robot is in cell(i, j)
(the starting cell).grid[i][j] == 0
indicates that the cell(i, j)
is blocked.grid[i][j] == 1
indicates that the cell(i, j)
is empty.grid[i][j] == 2
indicates that the cell(i, j)
is the target cell.
There is exactly one -1
and 2
in grid
. Remember that you will not have this information in your code.
Example 1:
Input: grid = [[1,2],[-1,0]] Output: 2 Explanation: One possible interaction is described below: The robot is initially standing on cell (1, 0), denoted by the -1. - master.canMove('U') returns true. - master.canMove('D') returns false. - master.canMove('L') returns false. - master.canMove('R') returns false. - master.move('U') moves the robot to the cell (0, 0). - 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('R') moves the robot to the cell (0, 1). - master.isTarget() returns true. We now know that the target is the cell (0, 1), and the shortest path to the target cell is 2.
Example 2:
Input: grid = [[0,0,-1],[1,1,1],[2,0,0]] Output: 4 Explanation: The minimum distance between the robot and the target cell is 4.
Example 3:
Input: grid = [[-1,0],[0,2]] Output: -1 Explanation: There is no path from the robot to the target cell.
Constraints:
1 <= n, m <= 500
m == grid.length
n == grid[i].length
grid[i][j]
is either-1
,0
,1
, or2
.- There is exactly one
-1
ingrid
. - There is exactly one
2
ingrid
.
题解:
When talking about shortest path, it is BFS.
But here it doesn't have the board, it only has a GridMaster with move functions that is like DFS.
Thus first perform DFS to find the target. Mark the block, path and target for each grid.
Then perform BFS from the same start position and find the position.
Time Complexity: O(N^2). N is the lenght of grid.
Space: O(N^2).
AC Java:
1 /** 2 * // This is the GridMaster's API interface. 3 * // You should not implement it, or speculate about its implementation 4 * class GridMaster { 5 * boolean canMove(char direction); 6 * void move(char direction); 7 * boolean isTarget(); 8 * } 9 */ 10 11 class Solution { 12 char [] forward = {'U','D','L','R'}; 13 char [] backward = {'D','U','R','L'}; 14 int[][] dirs = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; 15 int unvisited = 0; 16 int path = 1; 17 int block = -1; 18 int target = 2; 19 public int findShortestPath(GridMaster master) { 20 int N = 501; 21 int[][] grid = new int[2*N][2*N]; 22 dfs(N, N, master, grid); 23 if(grid[N][N] == target){ 24 return 0; 25 } 26 27 LinkedList<int[]> que = new LinkedList<>(); 28 que.add(new int[]{N, N}); 29 boolean[][] visited = new boolean[2*N][2*N]; 30 grid[N][N] = block; 31 int level = 0; 32 while(!que.isEmpty()){ 33 int size = que.size(); 34 while(size-- > 0){ 35 int [] cur = que.poll(); 36 37 for(int i = 0; i < 4; i++){ 38 int p = cur[0] + dirs[i][0]; 39 int q = cur[1] + dirs[i][1]; 40 if(grid[p][q] == target){ 41 return level + 1; 42 } 43 if(grid[p][q] == block){ 44 continue; 45 } 46 47 que.add(new int[]{p, q}); 48 grid[p][q] = block; 49 } 50 } 51 52 level++; 53 } 54 55 return -1; 56 } 57 58 private void dfs(int r, int c, GridMaster master, int[][] grid){ 59 if(grid[r][c] != unvisited){ 60 return; 61 } 62 63 if(master.isTarget()){ 64 grid[r][c] = target; 65 }else{ 66 grid[r][c] = path; 67 } 68 69 for(int i = 0; i < 4; i++){ 70 char f = forward[i]; 71 char b = backward[i]; 72 int p = r + dirs[i][0]; 73 int q = c + dirs[i][1]; 74 75 if(!master.canMove(f)){ 76 grid[p][q] = block; 77 }else{ 78 master.move(f); 79 dfs(p, q, master, grid); 80 master.move(b); 81 } 82 } 83 } 84 }