|
| 1 | +<h2><a href="https://leetcode.com/problems/nearest-exit-from-entrance-in-maze">2038. Nearest Exit from Entrance in Maze</a></h2><h3>Medium</h3><hr><p>You are given an <code>m x n</code> matrix <code>maze</code> (<strong>0-indexed</strong>) with empty cells (represented as <code>'.'</code>) and walls (represented as <code>'+'</code>). You are also given the <code>entrance</code> of the maze, where <code>entrance = [entrance<sub>row</sub>, entrance<sub>col</sub>]</code> denotes the row and column of the cell you are initially standing at.</p> |
| 2 | + |
| 3 | +<p>In one step, you can move one cell <strong>up</strong>, <strong>down</strong>, <strong>left</strong>, or <strong>right</strong>. You cannot step into a cell with a wall, and you cannot step outside the maze. Your goal is to find the <strong>nearest exit</strong> from the <code>entrance</code>. An <strong>exit</strong> is defined as an <strong>empty cell</strong> that is at the <strong>border</strong> of the <code>maze</code>. The <code>entrance</code> <strong>does not count</strong> as an exit.</p> |
| 4 | + |
| 5 | +<p>Return <em>the <strong>number of steps</strong> in the shortest path from the </em><code>entrance</code><em> to the nearest exit, or </em><code>-1</code><em> if no such path exists</em>.</p> |
| 6 | + |
| 7 | +<p> </p> |
| 8 | +<p><strong class="example">Example 1:</strong></p> |
| 9 | +<img alt="" src="https://assets.leetcode.com/uploads/2021/06/04/nearest1-grid.jpg" style="width: 333px; height: 253px;" /> |
| 10 | +<pre> |
| 11 | +<strong>Input:</strong> maze = [["+","+",".","+"],[".",".",".","+"],["+","+","+","."]], entrance = [1,2] |
| 12 | +<strong>Output:</strong> 1 |
| 13 | +<strong>Explanation:</strong> There are 3 exits in this maze at [1,0], [0,2], and [2,3]. |
| 14 | +Initially, you are at the entrance cell [1,2]. |
| 15 | +- You can reach [1,0] by moving 2 steps left. |
| 16 | +- You can reach [0,2] by moving 1 step up. |
| 17 | +It is impossible to reach [2,3] from the entrance. |
| 18 | +Thus, the nearest exit is [0,2], which is 1 step away. |
| 19 | +</pre> |
| 20 | + |
| 21 | +<p><strong class="example">Example 2:</strong></p> |
| 22 | +<img alt="" src="https://assets.leetcode.com/uploads/2021/06/04/nearesr2-grid.jpg" style="width: 253px; height: 253px;" /> |
| 23 | +<pre> |
| 24 | +<strong>Input:</strong> maze = [["+","+","+"],[".",".","."],["+","+","+"]], entrance = [1,0] |
| 25 | +<strong>Output:</strong> 2 |
| 26 | +<strong>Explanation:</strong> There is 1 exit in this maze at [1,2]. |
| 27 | +[1,0] does not count as an exit since it is the entrance cell. |
| 28 | +Initially, you are at the entrance cell [1,0]. |
| 29 | +- You can reach [1,2] by moving 2 steps right. |
| 30 | +Thus, the nearest exit is [1,2], which is 2 steps away. |
| 31 | +</pre> |
| 32 | + |
| 33 | +<p><strong class="example">Example 3:</strong></p> |
| 34 | +<img alt="" src="https://assets.leetcode.com/uploads/2021/06/04/nearest3-grid.jpg" style="width: 173px; height: 93px;" /> |
| 35 | +<pre> |
| 36 | +<strong>Input:</strong> maze = [[".","+"]], entrance = [0,0] |
| 37 | +<strong>Output:</strong> -1 |
| 38 | +<strong>Explanation:</strong> There are no exits in this maze. |
| 39 | +</pre> |
| 40 | + |
| 41 | +<p> </p> |
| 42 | +<p><strong>Constraints:</strong></p> |
| 43 | + |
| 44 | +<ul> |
| 45 | + <li><code>maze.length == m</code></li> |
| 46 | + <li><code>maze[i].length == n</code></li> |
| 47 | + <li><code>1 <= m, n <= 100</code></li> |
| 48 | + <li><code>maze[i][j]</code> is either <code>'.'</code> or <code>'+'</code>.</li> |
| 49 | + <li><code>entrance.length == 2</code></li> |
| 50 | + <li><code>0 <= entrance<sub>row</sub> < m</code></li> |
| 51 | + <li><code>0 <= entrance<sub>col</sub> < n</code></li> |
| 52 | + <li><code>entrance</code> will always be an empty cell.</li> |
| 53 | +</ul> |
0 commit comments