Skip to content

Commit de1c113

Browse files
committed
Sync LeetCode submission Runtime - 68 ms (71.70%), Memory - 24.5 MB (67.36%)
1 parent 958c0a1 commit de1c113

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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>&#39;*&#39;</code> is your location. There is <strong>exactly one </strong><code>&#39;*&#39;</code> cell.</li>
7+
<li><code>&#39;#&#39;</code> is a food cell. There may be <strong>multiple</strong> food cells.</li>
8+
<li><code>&#39;O&#39;</code> is free space, and you can travel through these cells.</li>
9+
<li><code>&#39;X&#39;</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>&nbsp;</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 = [[&quot;X&quot;,&quot;X&quot;,&quot;X&quot;,&quot;X&quot;,&quot;X&quot;,&quot;X&quot;],[&quot;X&quot;,&quot;*&quot;,&quot;O&quot;,&quot;O&quot;,&quot;O&quot;,&quot;X&quot;],[&quot;X&quot;,&quot;O&quot;,&quot;O&quot;,&quot;#&quot;,&quot;O&quot;,&quot;X&quot;],[&quot;X&quot;,&quot;X&quot;,&quot;X&quot;,&quot;X&quot;,&quot;X&quot;,&quot;X&quot;]]
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 = [[&quot;X&quot;,&quot;X&quot;,&quot;X&quot;,&quot;X&quot;,&quot;X&quot;],[&quot;X&quot;,&quot;*&quot;,&quot;X&quot;,&quot;O&quot;,&quot;X&quot;],[&quot;X&quot;,&quot;O&quot;,&quot;X&quot;,&quot;#&quot;,&quot;X&quot;],[&quot;X&quot;,&quot;X&quot;,&quot;X&quot;,&quot;X&quot;,&quot;X&quot;]]
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 = [[&quot;X&quot;,&quot;X&quot;,&quot;X&quot;,&quot;X&quot;,&quot;X&quot;,&quot;X&quot;,&quot;X&quot;,&quot;X&quot;],[&quot;X&quot;,&quot;*&quot;,&quot;O&quot;,&quot;X&quot;,&quot;O&quot;,&quot;#&quot;,&quot;O&quot;,&quot;X&quot;],[&quot;X&quot;,&quot;O&quot;,&quot;O&quot;,&quot;X&quot;,&quot;O&quot;,&quot;O&quot;,&quot;X&quot;,&quot;X&quot;],[&quot;X&quot;,&quot;O&quot;,&quot;O&quot;,&quot;O&quot;,&quot;O&quot;,&quot;#&quot;,&quot;O&quot;,&quot;X&quot;],[&quot;X&quot;,&quot;X&quot;,&quot;X&quot;,&quot;X&quot;,&quot;X&quot;,&quot;X&quot;,&quot;X&quot;,&quot;X&quot;]]
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>&nbsp;</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 &lt;= m, n &lt;= 200</code></li>
47+
<li><code>grid[row][col]</code> is <code>&#39;*&#39;</code>, <code>&#39;X&#39;</code>, <code>&#39;O&#39;</code>, or <code>&#39;#&#39;</code>.</li>
48+
<li>The <code>grid</code> contains <strong>exactly one</strong> <code>&#39;*&#39;</code>.</li>
49+
</ul>
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Approach 1: Breadth-First Search (BFS)
2+
3+
# m = no. of rows, n = no. of cols
4+
# Time: O(m * n)
5+
# Space: O(m * n)
6+
7+
from collections import deque
8+
9+
class Solution:
10+
def getFood(self, grid: List[List[str]]) -> int:
11+
dirs = [(0, 1), (0, -1), (-1, 0), (1, 0)]
12+
rows, cols = len(grid), len(grid[0])
13+
14+
start = next((i, j)
15+
for i in range(rows)
16+
for j in range(cols)
17+
if grid[i][j] == '*'
18+
)
19+
20+
queue = deque([start])
21+
steps = 1
22+
23+
while queue:
24+
for _ in range(len(queue)):
25+
row, col = queue.popleft()
26+
27+
for dx, dy in dirs:
28+
new_row, new_col = row + dx, col + dy
29+
30+
if self._is_valid(grid, new_row, new_col):
31+
if grid[new_row][new_col] == '#':
32+
return steps
33+
34+
grid[new_row][new_col] = 'X'
35+
queue.append((new_row, new_col))
36+
37+
steps += 1
38+
39+
return -1
40+
41+
def _is_valid(self, grid, row, col):
42+
return (
43+
0 <= row < len(grid)
44+
and 0 <= col < len(grid[0])
45+
and grid[row][col] != 'X'
46+
)

0 commit comments

Comments
 (0)