java.lang.Object
g1901_2000.s1926_nearest_exit_from_entrance_in_maze.Solution

public class Solution extends Object
1926 - Nearest Exit from Entrance in Maze.<p>Medium</p> <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> <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> <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> <p><strong>Example 1:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2021/06/04/nearest1-grid.jpg" alt="" /></p> <p><strong>Input:</strong> maze = [[&ldquo;+&rdquo;,&ldquo;+&rdquo;,&ldquo;.&rdquo;,&ldquo;+&rdquo;],[&ldquo;.&rdquo;,&ldquo;.&rdquo;,&ldquo;.&rdquo;,&ldquo;+&rdquo;],[&ldquo;+&rdquo;,&ldquo;+&rdquo;,&ldquo;+&rdquo;,&ldquo;.&rdquo;]], entrance = [1,2]</p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>There are 3 exits in this maze at [1,0], [0,2], and [2,3]. Initially, you are at the entrance cell [1,2].</p> <ul> <li> <p>You can reach [1,0] by moving 2 steps left.</p> </li> <li> <p>You can reach [0,2] by moving 1 step up.</p> </li> </ul> <p>It is impossible to reach [2,3] from the entrance. Thus, the nearest exit is [0,2], which is 1 step away.</p> <p><strong>Example 2:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2021/06/04/nearesr2-grid.jpg" alt="" /></p> <p><strong>Input:</strong> maze = [[&ldquo;+&rdquo;,&ldquo;+&rdquo;,&ldquo;+&rdquo;],[&ldquo;.&rdquo;,&ldquo;.&rdquo;,&ldquo;.&rdquo;],[&ldquo;+&rdquo;,&ldquo;+&rdquo;,&ldquo;+&rdquo;]], entrance = [1,0]</p> <p><strong>Output:</strong> 2</p> <p><strong>Explanation:</strong></p> <p>There is 1 exit in this maze at [1,2]. [1,0] does not count as an exit since it is the entrance cell. Initially, you are at the entrance cell [1,0].</p> <ul> <li>You can reach [1,2] by moving 2 steps right.</li> </ul> <p>Thus, the nearest exit is [1,2], which is 2 steps away.</p> <p><strong>Example 3:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2021/06/04/nearest3-grid.jpg" alt="" /></p> <p><strong>Input:</strong> maze = [[&ldquo;.&rdquo;,&ldquo;+&rdquo;]], entrance = [0,0]</p> <p><strong>Output:</strong> -1</p> <p><strong>Explanation:</strong> There are no exits in this maze.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>maze.length == m</code></li> <li><code>maze[i].length == n</code></li> <li><code>1 <= m, n <= 100</code></li> <li><code>maze[i][j]</code> is either <code>'.'</code> or <code>'+'</code>.</li> <li><code>entrance.length == 2</code></li> <li><code>0 <= entrance<sub>row</sub> < m</code></li> <li><code>0 <= entrance<sub>col</sub> < n</code></li> <li><code>entrance</code> will always be an empty cell.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • nearestExit

      public int nearestExit(char[][] maze, int[] entrance)