-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsurrounded_regions.go
71 lines (63 loc) · 1.34 KB
/
surrounded_regions.go
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package main
func solve(board [][]byte) {
m := len(board)
if m < 3 {
return
}
n := len(board[0])
if n < 3 {
return
}
o := byte('o')
x := byte('x')
notSurrounded := make([][]bool, len(board))
for i := 0; i < m; i++ {
notSurrounded[i] = make([]bool, n)
}
var dfs func(x, y int)
dfs = func(x, y int) {
if x > 0 && board[x-1][y] == o && !notSurrounded[x-1][y] {
notSurrounded[x-1][y] = true
dfs(x-1, y)
}
if x < m-1 && board[x+1][y] == o && !notSurrounded[x+1][y] {
notSurrounded[x+1][y] = true
dfs(x+1, y)
}
if y > 0 && board[x][y-1] == o && !notSurrounded[x][y-1] {
notSurrounded[x][y-1] = true
dfs(x, y-1)
}
if y < n-1 && board[x][y+1] == o && !notSurrounded[x][y+1] {
notSurrounded[x][y+1] = true
dfs(x, y+1)
}
}
for i := 0; i < m; i++ {
if board[i][0] == o && !notSurrounded[i][0] {
notSurrounded[i][0] = true
dfs(i, 0)
}
if board[i][n-1] == o && !notSurrounded[i][n-1] {
notSurrounded[i][n-1] = true
dfs(i, n-1)
}
}
for j := 1; j < n-1; j++ {
if board[0][j] == o && !notSurrounded[0][j] {
notSurrounded[0][j] = true
dfs(0, j)
}
if board[m-1][j] == o && !notSurrounded[m-1][j] {
notSurrounded[m-1][j] = true
dfs(m-1, j)
}
}
for i := 0; i < m; i++ {
for j := 0; j < n; j++ {
if board[i][j] == o && !notSurrounded[i][j] {
board[i][j] = x
}
}
}
}