Skip to content

Commit

Permalink
Add a solution to Shortest Path to Get Food
Browse files Browse the repository at this point in the history
  • Loading branch information
soapyigu committed Sep 14, 2022
1 parent abc922a commit 34b53ab
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions BFS/ShortestPathGetFood.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* Question Link: https://leetcode.com/problems/shortest-path-to-get-food/
* Primary idea: BFS to go over all possible word paths until the cell is food, avoid hitting the obstacle.
*
* Time Complexity: O(nm), Space Complexity: O(nm)
*/

class ShortestPathGetFood {
func getFood(_ grid: [[Character]]) -> Int {
let m = grid.count, n = grid[0].count
var isVisited = Array(repeating: Array(repeating: false, count: n), count: m)

let start = findStart(grid)

isVisited[start.0][start.1] = true
var queue = [Point(i: start.0, j: start.1, len: 0)]

while !queue.isEmpty {
let point = queue.removeFirst()

if grid[point.i][point.j] == "#" {
return point.len
}

for dir in [(0, 1), (0, -1), (1, 0), (-1, 0)] {
let (x, y) = (point.i + dir.0, point.j + dir.1)

guard x >= 0 && x < m && y >= 0 && y < n && !isVisited[x][y] else {
continue
}

if grid[x][y] == "X" {
continue
}

isVisited[x][y] = true
queue.append(Point(i: x, j: y, len: point.len + 1))
}
}

return -1
}

private func findStart(_ grid: [[Character]]) -> (Int, Int) {
for i in 0..<grid.count {
for j in 0..<grid[0].count {
if grid[i][j] == "*" {
return (i, j)
}
}
}

return (-1, -1)
}

struct Point {
var i: Int
var j: Int
var len: Int
}
}

0 comments on commit 34b53ab

Please sign in to comment.