comments | difficulty | edit_url | tags | ||||
---|---|---|---|---|---|---|---|
true |
Medium |
|
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
.
We can assume that the robot starts from the coordinate
If the endpoint cannot be found, we directly return
The time complexity is
Similar problems:
# """
# This is GridMaster's API interface.
# You should not implement it, or speculate about its implementation
# """
# class GridMaster(object):
# def canMove(self, direction: str) -> bool:
#
#
# def move(self, direction: str) -> bool:
#
#
# def isTarget(self) -> None:
#
#
class Solution(object):
def findShortestPath(self, master: "GridMaster") -> int:
def dfs(i: int, j: int):
if master.isTarget():
nonlocal target
target = (i, j)
return
for k, c in enumerate(s):
x, y = i + dirs[k], j + dirs[k + 1]
if master.canMove(c) and (x, y) not in vis:
vis.add((x, y))
master.move(c)
dfs(x, y)
master.move(s[(k + 2) % 4])
s = "URDL"
dirs = (-1, 0, 1, 0, -1)
target = None
vis = set()
dfs(0, 0)
if target is None:
return -1
vis.discard((0, 0))
q = deque([(0, 0)])
ans = -1
while q:
ans += 1
for _ in range(len(q)):
i, j = q.popleft()
if (i, j) == target:
return ans
for a, b in pairwise(dirs):
x, y = i + a, j + b
if (x, y) in vis:
vis.remove((x, y))
q.append((x, y))
return -1
/**
* // This is the GridMaster's API interface.
* // You should not implement it, or speculate about its implementation
* class GridMaster {
* boolean canMove(char direction);
* void move(char direction);
* boolean isTarget();
* }
*/
class Solution {
private int[] target;
private GridMaster master;
private final int n = 2010;
private final String s = "URDL";
private final int[] dirs = {-1, 0, 1, 0, -1};
private final Set<Integer> vis = new HashSet<>();
public int findShortestPath(GridMaster master) {
this.master = master;
dfs(0, 0);
if (target == null) {
return -1;
}
vis.remove(0);
Deque<int[]> q = new ArrayDeque<>();
q.offer(new int[] {0, 0});
for (int ans = 0; !q.isEmpty(); ++ans) {
for (int m = q.size(); m > 0; --m) {
var p = q.poll();
if (p[0] == target[0] && p[1] == target[1]) {
return ans;
}
for (int k = 0; k < 4; ++k) {
int x = p[0] + dirs[k], y = p[1] + dirs[k + 1];
if (vis.remove(x * n + y)) {
q.offer(new int[] {x, y});
}
}
}
}
return -1;
}
private void dfs(int i, int j) {
if (master.isTarget()) {
target = new int[] {i, j};
return;
}
for (int k = 0; k < 4; ++k) {
int x = i + dirs[k], y = j + dirs[k + 1];
if (master.canMove(s.charAt(k)) && vis.add(x * n + y)) {
master.move(s.charAt(k));
dfs(x, y);
master.move(s.charAt((k + 2) % 4));
}
}
}
}
/**
* // 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 {
private:
const int n = 2010;
int dirs[5] = {-1, 0, 1, 0, -1};
string s = "URDL";
int target;
unordered_set<int> vis;
public:
int findShortestPath(GridMaster& master) {
target = n * n;
vis.insert(0);
dfs(0, 0, master);
if (target == n * n) {
return -1;
}
vis.erase(0);
queue<pair<int, int>> q;
q.emplace(0, 0);
for (int ans = 0; q.size(); ++ans) {
for (int m = q.size(); m; --m) {
auto [i, j] = q.front();
q.pop();
if (i * n + j == target) {
return ans;
}
for (int k = 0; k < 4; ++k) {
int x = i + dirs[k], y = j + dirs[k + 1];
if (vis.count(x * n + y)) {
vis.erase(x * n + y);
q.emplace(x, y);
}
}
}
}
return -1;
}
void dfs(int i, int j, GridMaster& master) {
if (master.isTarget()) {
target = i * n + j;
}
for (int k = 0; k < 4; ++k) {
int x = i + dirs[k], y = j + dirs[k + 1];
if (master.canMove(s[k]) && !vis.count(x * n + y)) {
vis.insert(x * n + y);
master.move(s[k]);
dfs(x, y, master);
master.move(s[(k + 2) % 4]);
}
}
}
};