[LeetCode] Shortest Path in a Hidden Grid

1778. Shortest Path in a Hidden Grid

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) Returns true if the robot can move in that direction. Otherwise, it returns false.
  • 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() 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 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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/**
* // This is the GridMaster's API interface.
* // You should not implement it, or speculate about its implementation
* class GridMaster {
* public:
* bool canMove(char direction);
* void move(char direction);
* boolean isTarget();
* };
*/

class Solution {
unordered_map<int, unordered_set<int>> g;
int ty = INT_MAX, tx = INT_MAX;
char d[4]{'U','L','D','R'};
int dy[4]{-1,0,1,0},dx[4]{0,-1,0,1};
bool vis(int y, int x) {
return g.count(y) and g[y].count(x);
}
void rollBack(GridMaster& master, int dir) {
if(dir == -1) return;
master.move(d[(dir + 2)%4]);
}
void dfs(int y, int x, GridMaster &master, int dir) {
g[y].insert(x);
if(master.isTarget()) {
ty = y, tx = x;
}
for(int i = 0; i < 4; i++) {
if(!master.canMove(d[i]) or vis(y + dy[i], x + dx[i])) continue;
master.move(d[i]);
dfs(y + dy[i], x + dx[i], master, i);
}

rollBack(master, dir);
}
int bfs() {
queue<pair<int, int>> q;
q.push({0,0});
int res = 1;
while(!q.empty()) {
int sz = q.size();
while(sz--) {
auto [y,x] = q.front(); q.pop();
for(int i = 0; i < 4; i++) {
int ny = y + dy[i], nx = x + dx[i];
if(g.count(ny) and g[ny].count(nx)) {
g[ny].erase(nx);
if(ny == ty and nx == tx) return res;
q.push({ny,nx});
}
}
}
res++;
}
return res;
}
public:
int findShortestPath(GridMaster &master) {
dfs(0,0,master,-1);
if(ty == INT_MAX) return -1;
return bfs();
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/07/12/PS/LeetCode/shortest-path-in-a-hidden-grid/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.