Skip to content

Commit

Permalink
Refactor code style for BFS questions
Browse files Browse the repository at this point in the history
  • Loading branch information
soapyigu committed Sep 23, 2022
1 parent 12f4ff1 commit 346ab14
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 49 deletions.
45 changes: 23 additions & 22 deletions BFS/ShortestPathGetFood.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,36 @@ class ShortestPathGetFood {
let start = findStart(grid)

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

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

if grid[point.i][point.j] == "#" {
return point.len
}
let size = queue.count

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
for _ in 0..<size {
let point = queue.removeFirst()

if grid[point.0][point.1] == "#" {
return count
}

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

for dir in [(0, 1), (0, -1), (1, 0), (-1, 0)] {
let (x, y) = (point.0 + dir.0, point.1 + 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((x, y))
}

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

count += 1
}

return -1
Expand All @@ -52,10 +59,4 @@ class ShortestPathGetFood {

return (-1, -1)
}

struct Point {
var i: Int
var j: Int
var len: Int
}
}
51 changes: 25 additions & 26 deletions BFS/ShortestPathGridObstaclesElimination.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,56 +8,55 @@
class ShortestPathGridObstaclesElimination {
func shortestPath(_ grid: [[Int]], _ k: Int) -> Int {
let m = grid.count, n = grid[0].count
var remainings = Array(repeating: Array(repeating: -1, count: n), count: m)
var remaining = Array(repeating: Array(repeating: -1, count: n), count: m)
var queue = [Point(i: 0, j: 0, remain: k)], count = 0

remainings[0][0] = k
var queue = [Point(i: 0, j: 0, remain: k)]
var count = 0

if k > m + n - 2 {
return m + n - 2;
if m + n - 2 < k {
return m + n - 2
}

while !queue.isEmpty {
let size = queue.count

let currentPoints = queue
queue.removeAll()

for point in currentPoints {
if point.i == m - 1 && point.j == n - 1 {
for _ in 0..<size {
let point = queue.removeFirst()

if point.i == m - 1, point.j == n - 1 {
return count
}

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

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 else {
continue
}

if grid[x][y] == 1 && point.remain <= 0 {
if grid[x][y] == 1 && point.remain == 0 {
continue
}

let remain = grid[x][y] == 1 ? point.remain - 1 : point.remain
// only choose the path if remainings are greater
if remainings[x][y] >= remain {
let nextRemaining = grid[x][y] == 1 ? point.remain - 1 : point.remain

if remaining[x][y] >= nextRemaining {
continue
}
}

remainings[x][y] = remain
queue.append(Point(i: x, j: y, remain: remain))
remaining[x][y] = nextRemaining
queue.append(Point(i: x, j: y, remain: nextRemaining))

}
}

count += 1
}

return -1
}

struct Point {
var i: Int
var j: Int
var remain: Int
let i: Int
let j: Int
let remain: Int
}
}
2 changes: 1 addition & 1 deletion BFS/WordLadder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class WordLadder {

let size = wordQueue.count

for i in 0..<size {
for _ in 0..<size {
let currentWord = wordQueue.removeFirst()

if currentWord == endWord {
Expand Down

0 comments on commit 346ab14

Please sign in to comment.