-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
0130-surrounded-regions.swift
48 lines (42 loc) · 1.38 KB
/
0130-surrounded-regions.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
class Solution {
func solve(_ board: inout [[Character]]) {
var rows = board.count
var columns = board[0].count
// 1. DFS Capture unsurrounded regions (O -> T)
for r in 0..<rows {
for c in 0..<columns {
if board[r][c] == "O" &&
([0, rows - 1].contains(r) || [0, columns - 1].contains(c)) {
capture(r, c, &board)
}
}
}
// 2. Capture surrounded regions (O -> X)
for r in 0..<rows {
for c in 0..<columns {
if board[r][c] == "O" {
board[r][c] = Character("X")
}
}
}
// 3. Uncapture surrounded regions (T -> O)
for r in 0..<rows {
for c in 0..<columns {
if board[r][c] == "T" {
board[r][c] = Character("O")
}
}
}
}
func capture(_ r: Int, _ c: Int, _ board: inout [[Character]]) {
// Make sure r and c are in bounds
guard r >= 0, r < board.count, c >= 0, c < board[0].count, board[r][c] == "O" else {
return
}
board[r][c] = Character("T")
capture(r - 1, c, &board)
capture(r + 1, c, &board)
capture(r, c - 1, &board)
capture(r, c + 1, &board)
}
}