|
| 1 | +<h2><a href="https://leetcode.com/problems/the-maze-ii">505. The Maze II</a></h2><h3>Medium</h3><hr><p>There is a ball in a <code>maze</code> with empty spaces (represented as <code>0</code>) and walls (represented as <code>1</code>). The ball can go through the empty spaces by rolling <strong>up, down, left or right</strong>, but it won't stop rolling until hitting a wall. When the ball stops, it could choose the next direction.</p> |
| 2 | + |
| 3 | +<p>Given the <code>m x n</code> <code>maze</code>, the ball's <code>start</code> position and the <code>destination</code>, where <code>start = [start<sub>row</sub>, start<sub>col</sub>]</code> and <code>destination = [destination<sub>row</sub>, destination<sub>col</sub>]</code>, return <em>the shortest <strong>distance</strong> for the ball to stop at the destination</em>. If the ball cannot stop at <code>destination</code>, return <code>-1</code>.</p> |
| 4 | + |
| 5 | +<p>The <strong>distance</strong> is the number of <strong>empty spaces</strong> traveled by the ball from the start position (excluded) to the destination (included).</p> |
| 6 | + |
| 7 | +<p>You may assume that <strong>the borders of the maze are all walls</strong> (see examples).</p> |
| 8 | + |
| 9 | +<p> </p> |
| 10 | +<p><strong class="example">Example 1:</strong></p> |
| 11 | +<img alt="" src="https://assets.leetcode.com/uploads/2021/03/31/maze1-1-grid.jpg" style="width: 573px; height: 573px;" /> |
| 12 | +<pre> |
| 13 | +<strong>Input:</strong> maze = [[0,0,1,0,0],[0,0,0,0,0],[0,0,0,1,0],[1,1,0,1,1],[0,0,0,0,0]], start = [0,4], destination = [4,4] |
| 14 | +<strong>Output:</strong> 12 |
| 15 | +<strong>Explanation:</strong> One possible way is : left -> down -> left -> down -> right -> down -> right. |
| 16 | +The length of the path is 1 + 1 + 3 + 1 + 2 + 2 + 2 = 12. |
| 17 | +</pre> |
| 18 | + |
| 19 | +<p><strong class="example">Example 2:</strong></p> |
| 20 | +<img alt="" src="https://assets.leetcode.com/uploads/2021/03/31/maze1-2-grid.jpg" style="width: 573px; height: 573px;" /> |
| 21 | +<pre> |
| 22 | +<strong>Input:</strong> maze = [[0,0,1,0,0],[0,0,0,0,0],[0,0,0,1,0],[1,1,0,1,1],[0,0,0,0,0]], start = [0,4], destination = [3,2] |
| 23 | +<strong>Output:</strong> -1 |
| 24 | +<strong>Explanation:</strong> There is no way for the ball to stop at the destination. Notice that you can pass through the destination but you cannot stop there. |
| 25 | +</pre> |
| 26 | + |
| 27 | +<p><strong class="example">Example 3:</strong></p> |
| 28 | + |
| 29 | +<pre> |
| 30 | +<strong>Input:</strong> maze = [[0,0,0,0,0],[1,1,0,0,1],[0,0,0,0,0],[0,1,0,0,1],[0,1,0,0,0]], start = [4,3], destination = [0,1] |
| 31 | +<strong>Output:</strong> -1 |
| 32 | +</pre> |
| 33 | + |
| 34 | +<p> </p> |
| 35 | +<p><strong>Constraints:</strong></p> |
| 36 | + |
| 37 | +<ul> |
| 38 | + <li><code>m == maze.length</code></li> |
| 39 | + <li><code>n == maze[i].length</code></li> |
| 40 | + <li><code>1 <= m, n <= 100</code></li> |
| 41 | + <li><code>maze[i][j]</code> is <code>0</code> or <code>1</code>.</li> |
| 42 | + <li><code>start.length == 2</code></li> |
| 43 | + <li><code>destination.length == 2</code></li> |
| 44 | + <li><code>0 <= start<sub>row</sub>, destination<sub>row</sub> < m</code></li> |
| 45 | + <li><code>0 <= start<sub>col</sub>, destination<sub>col</sub> < n</code></li> |
| 46 | + <li>Both the ball and the destination exist in an empty space, and they will not be in the same position initially.</li> |
| 47 | + <li>The maze contains <strong>at least 2 empty spaces</strong>.</li> |
| 48 | +</ul> |
0 commit comments