|
| 1 | +<p>This is an <strong>interactive problem</strong>.</p> |
| 2 | + |
| 3 | +<p>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 <code>m x n</code>, and each cell in the grid is either empty or blocked. It is <strong>guaranteed</strong> that the starting cell and the target cell are different, and neither of them is blocked.</p> |
| 4 | + |
| 5 | +<p>You want to find the minimum distance to the target cell. However, you <strong>do not know</strong> the grid's dimensions, the starting cell, nor the target cell. You are only allowed to ask queries to the <code>GridMaster</code> object.</p> |
| 6 | + |
| 7 | +<p>Thr <code>GridMaster</code> class has the following functions:</p> |
| 8 | + |
| 9 | +<ul> |
| 10 | + <li><code>boolean canMove(char direction)</code> Returns <code>true</code> if the robot can move in that direction. Otherwise, it returns <code>false</code>.</li> |
| 11 | + <li><code>void move(char direction)</code> 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 <strong>ignored</strong>, and the robot will remain in the same position.</li> |
| 12 | + <li><code>boolean isTarget()</code> Returns <code>true</code> if the robot is currently on the target cell. Otherwise, it returns <code>false</code>.</li> |
| 13 | +</ul> |
| 14 | + |
| 15 | +<p>Note that <code>direction</code> in the above functions should be a character from <code>{'U','D','L','R'}</code>, representing the directions up, down, left, and right, respectively.</p> |
| 16 | + |
| 17 | +<p>Return <em>the <strong>minimum distance</strong> between the robot's initial starting cell and the target cell. If there is no valid path between the cells, return </em><code>-1</code>.</p> |
| 18 | + |
| 19 | +<p><strong>Custom testing:</strong></p> |
| 20 | + |
| 21 | +<p>The test input is read as a 2D matrix <code>grid</code> of size <code>m x n</code> where:</p> |
| 22 | + |
| 23 | +<ul> |
| 24 | + <li><code>grid[i][j] == -1</code> indicates that the robot is in cell <code>(i, j)</code> (the starting cell).</li> |
| 25 | + <li><code>grid[i][j] == 0</code> indicates that the cell <code>(i, j)</code> is blocked.</li> |
| 26 | + <li><code>grid[i][j] == 1</code> indicates that the cell <code>(i, j)</code> is empty.</li> |
| 27 | + <li><code>grid[i][j] == 2</code> indicates that the cell <code>(i, j)</code> is the target cell.</li> |
| 28 | +</ul> |
| 29 | + |
| 30 | +<p>There is exactly one <code>-1</code> and <code>2</code> in <code>grid</code>. Remember that you will <strong>not</strong> have this information in your code.</p> |
| 31 | + |
| 32 | +<p> </p> |
| 33 | +<p><strong class="example">Example 1:</strong></p> |
| 34 | + |
| 35 | +<pre> |
| 36 | +<strong>Input:</strong> grid = [[1,2],[-1,0]] |
| 37 | +<strong>Output:</strong> 2 |
| 38 | +<strong>Explanation:</strong> One possible interaction is described below: |
| 39 | +The robot is initially standing on cell (1, 0), denoted by the -1. |
| 40 | +- master.canMove('U') returns true. |
| 41 | +- master.canMove('D') returns false. |
| 42 | +- master.canMove('L') returns false. |
| 43 | +- master.canMove('R') returns false. |
| 44 | +- master.move('U') moves the robot to the cell (0, 0). |
| 45 | +- master.isTarget() returns false. |
| 46 | +- master.canMove('U') returns false. |
| 47 | +- master.canMove('D') returns true. |
| 48 | +- master.canMove('L') returns false. |
| 49 | +- master.canMove('R') returns true. |
| 50 | +- master.move('R') moves the robot to the cell (0, 1). |
| 51 | +- master.isTarget() returns true. |
| 52 | +We now know that the target is the cell (0, 1), and the shortest path to the target cell is 2. |
| 53 | +</pre> |
| 54 | + |
| 55 | +<p><strong class="example">Example 2:</strong></p> |
| 56 | + |
| 57 | +<pre> |
| 58 | +<strong>Input:</strong> grid = [[0,0,-1],[1,1,1],[2,0,0]] |
| 59 | +<strong>Output:</strong> 4 |
| 60 | +<strong>Explanation:</strong> The minimum distance between the robot and the target cell is 4.</pre> |
| 61 | + |
| 62 | +<p><strong class="example">Example 3:</strong></p> |
| 63 | + |
| 64 | +<pre> |
| 65 | +<strong>Input:</strong> grid = [[-1,0],[0,2]] |
| 66 | +<strong>Output:</strong> -1 |
| 67 | +<strong>Explanation:</strong> There is no path from the robot to the target cell.</pre> |
| 68 | + |
| 69 | +<p> </p> |
| 70 | +<p><strong>Constraints:</strong></p> |
| 71 | + |
| 72 | +<ul> |
| 73 | + <li><code>1 <= n, m <= 500</code></li> |
| 74 | + <li><code>m == grid.length</code></li> |
| 75 | + <li><code>n == grid[i].length</code></li> |
| 76 | + <li><code>grid[i][j]</code> is either <code>-1</code>, <code>0</code>, <code>1</code>, or <code>2</code>.</li> |
| 77 | + <li>There is <strong>exactly one</strong> <code>-1</code> in <code>grid</code>.</li> |
| 78 | + <li>There is <strong>exactly one</strong> <code>2</code> in <code>grid</code>.</li> |
| 79 | +</ul> |
0 commit comments