|
| 1 | +<p>You are starving and you want to eat food as quickly as possible. You want to find the shortest path to arrive at any food cell.</p> |
| 2 | + |
| 3 | +<p>You are given an <code>m x n</code> character matrix, <code>grid</code>, of these different types of cells:</p> |
| 4 | + |
| 5 | +<ul> |
| 6 | + <li><code>'*'</code> is your location. There is <strong>exactly one </strong><code>'*'</code> cell.</li> |
| 7 | + <li><code>'#'</code> is a food cell. There may be <strong>multiple</strong> food cells.</li> |
| 8 | + <li><code>'O'</code> is free space, and you can travel through these cells.</li> |
| 9 | + <li><code>'X'</code> is an obstacle, and you cannot travel through these cells.</li> |
| 10 | +</ul> |
| 11 | + |
| 12 | +<p>You can travel to any adjacent cell north, east, south, or west of your current location if there is not an obstacle.</p> |
| 13 | + |
| 14 | +<p>Return <em>the <strong>length</strong> of the shortest path for you to reach <strong>any</strong> food cell</em>. If there is no path for you to reach food, return <code>-1</code>.</p> |
| 15 | + |
| 16 | +<p> </p> |
| 17 | +<p><strong class="example">Example 1:</strong></p> |
| 18 | +<img alt="" src="https://assets.leetcode.com/uploads/2020/09/21/img1.jpg" style="width: 300px; height: 201px;" /> |
| 19 | +<pre> |
| 20 | +<strong>Input:</strong> grid = [["X","X","X","X","X","X"],["X","*","O","O","O","X"],["X","O","O","#","O","X"],["X","X","X","X","X","X"]] |
| 21 | +<strong>Output:</strong> 3 |
| 22 | +<strong>Explanation:</strong> It takes 3 steps to reach the food. |
| 23 | +</pre> |
| 24 | + |
| 25 | +<p><strong class="example">Example 2:</strong></p> |
| 26 | +<img alt="" src="https://assets.leetcode.com/uploads/2020/09/21/img2.jpg" style="width: 300px; height: 241px;" /> |
| 27 | +<pre> |
| 28 | +<strong>Input:</strong> grid = [["X","X","X","X","X"],["X","*","X","O","X"],["X","O","X","#","X"],["X","X","X","X","X"]] |
| 29 | +<strong>Output:</strong> -1 |
| 30 | +<strong>Explanation:</strong> It is not possible to reach the food. |
| 31 | +</pre> |
| 32 | + |
| 33 | +<p><strong class="example">Example 3:</strong></p> |
| 34 | +<img alt="" src="https://assets.leetcode.com/uploads/2020/09/21/img3.jpg" style="width: 300px; height: 188px;" /> |
| 35 | +<pre> |
| 36 | +<strong>Input:</strong> grid = [["X","X","X","X","X","X","X","X"],["X","*","O","X","O","#","O","X"],["X","O","O","X","O","O","X","X"],["X","O","O","O","O","#","O","X"],["X","X","X","X","X","X","X","X"]] |
| 37 | +<strong>Output:</strong> 6 |
| 38 | +<strong>Explanation:</strong> There can be multiple food cells. It only takes 6 steps to reach the bottom food.</pre> |
| 39 | + |
| 40 | +<p> </p> |
| 41 | +<p><strong>Constraints:</strong></p> |
| 42 | + |
| 43 | +<ul> |
| 44 | + <li><code>m == grid.length</code></li> |
| 45 | + <li><code>n == grid[i].length</code></li> |
| 46 | + <li><code>1 <= m, n <= 200</code></li> |
| 47 | + <li><code>grid[row][col]</code> is <code>'*'</code>, <code>'X'</code>, <code>'O'</code>, or <code>'#'</code>.</li> |
| 48 | + <li>The <code>grid</code> contains <strong>exactly one</strong> <code>'*'</code>.</li> |
| 49 | +</ul> |
0 commit comments