-
Notifications
You must be signed in to change notification settings - Fork 6
/
test.go
154 lines (134 loc) · 2.98 KB
/
test.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package main
import "fmt"
// findOBoundaries return coordinates that value is 'O'
// and mark the coordinates visited
func findOBoundaries(board [][]byte, oVisited *[][]int) [][]int {
var boundaries [][]int
if 1 == len(board) {
if 'O' == board[0][0] {
p := []int{0, 0}
boundaries = append(boundaries, p)
*oVisited = append(*oVisited, p)
}
return boundaries
}
// left boundary
for i := 0; i < len(board)-1; i++ {
if 'O' == board[i][0] {
p := []int{i, 0}
boundaries = append(boundaries, p)
*oVisited = append(*oVisited, p)
}
}
// bottom boundary
for j := 0; j < len(board[0])-1; j++ {
if 'O' == board[len(board)-1][j] {
p := []int{len(board) - 1, j}
boundaries = append(boundaries, p)
*oVisited = append(*oVisited, p)
}
}
// right boundary
for i := len(board) - 1; i > 0; i-- {
if 'O' == board[i][len(board[0])-1] {
p := []int{i, len(board[0]) - 1}
boundaries = append(boundaries, p)
*oVisited = append(*oVisited, p)
}
}
// top boundary
for j := len(board[0]) - 1; j > 0; j-- {
if 'O' == board[0][j] {
p := []int{0, j}
boundaries = append(boundaries, p)
*oVisited = append(*oVisited, p)
}
}
return boundaries
}
func isVisited(i, j int, oVisited *[][]int) bool {
for _, v := range *oVisited {
if v[0] == i && v[1] == j {
return true
}
}
return false
}
func findOsAround(board [][]byte, i, j int, oVisited *[][]int) [][]int {
var osAround [][]int
// left
if j > 0 {
left := board[i][j-1]
if !isVisited(i, j-1, oVisited) && 'O' == left {
p := []int{i, j - 1}
osAround = append(osAround, p)
*oVisited = append(*oVisited, p)
}
}
// down
if i < len(board)-1 {
down := board[i+1][j]
if !isVisited(i+1, j, oVisited) && 'O' == down {
p := []int{i + 1, j}
osAround = append(osAround, p)
*oVisited = append(*oVisited, p)
}
}
// right
if j < len(board[0])-1 {
right := board[i][j+1]
if !isVisited(i, j+1, oVisited) && 'O' == right {
p := []int{i, j + 1}
osAround = append(osAround, p)
*oVisited = append(*oVisited, p)
}
}
// up
if i > 0 {
up := board[i-1][j]
if !isVisited(i-1, j, oVisited) && 'O' == up {
p := []int{i - 1, j}
osAround = append(osAround, p)
*oVisited = append(*oVisited, p)
}
}
return osAround
}
func findOs(board [][]byte, points [][]int, oVisited *[][]int) [][]int {
var os [][]int
for _, p := range points {
os = append(os, findOsAround(board, p[0], p[1], oVisited)...)
}
return os
}
func solve(board [][]byte) {
if 0 == len(board) {
return
}
var oVisited [][]int
// visit all 'O's from boundaries that is 'O'
os := findOBoundaries(board, &oVisited)
for {
os = findOs(board, os, &oVisited)
if len(os) == 0 {
break
}
}
// alter the not visited 'O' to 'X'
for i := range board {
for j := range board[i] {
if 'O' == board[i][j] && !isVisited(i, j, &oVisited) {
board[i][j] = 'X'
}
}
}
}
func main() {
board := [][]byte{
{'O', 'X', 'O'},
{'X', 'O', 'X'},
{'O', 'X', 'O'},
}
solve(board)
fmt.Println(board)
}