Skip to content

Commit 34b53ab

Browse files
committed
Add a solution to Shortest Path to Get Food
1 parent abc922a commit 34b53ab

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

BFS/ShortestPathGetFood.swift

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/**
2+
* Question Link: https://leetcode.com/problems/shortest-path-to-get-food/
3+
* Primary idea: BFS to go over all possible word paths until the cell is food, avoid hitting the obstacle.
4+
*
5+
* Time Complexity: O(nm), Space Complexity: O(nm)
6+
*/
7+
8+
class ShortestPathGetFood {
9+
func getFood(_ grid: [[Character]]) -> Int {
10+
let m = grid.count, n = grid[0].count
11+
var isVisited = Array(repeating: Array(repeating: false, count: n), count: m)
12+
13+
let start = findStart(grid)
14+
15+
isVisited[start.0][start.1] = true
16+
var queue = [Point(i: start.0, j: start.1, len: 0)]
17+
18+
while !queue.isEmpty {
19+
let point = queue.removeFirst()
20+
21+
if grid[point.i][point.j] == "#" {
22+
return point.len
23+
}
24+
25+
for dir in [(0, 1), (0, -1), (1, 0), (-1, 0)] {
26+
let (x, y) = (point.i + dir.0, point.j + dir.1)
27+
28+
guard x >= 0 && x < m && y >= 0 && y < n && !isVisited[x][y] else {
29+
continue
30+
}
31+
32+
if grid[x][y] == "X" {
33+
continue
34+
}
35+
36+
isVisited[x][y] = true
37+
queue.append(Point(i: x, j: y, len: point.len + 1))
38+
}
39+
}
40+
41+
return -1
42+
}
43+
44+
private func findStart(_ grid: [[Character]]) -> (Int, Int) {
45+
for i in 0..<grid.count {
46+
for j in 0..<grid[0].count {
47+
if grid[i][j] == "*" {
48+
return (i, j)
49+
}
50+
}
51+
}
52+
53+
return (-1, -1)
54+
}
55+
56+
struct Point {
57+
var i: Int
58+
var j: Int
59+
var len: Int
60+
}
61+
}

0 commit comments

Comments
 (0)