-
Notifications
You must be signed in to change notification settings - Fork 23
/
130.go
81 lines (64 loc) · 1.48 KB
/
130.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
package problem
/*
给定一个二维的矩阵,包含 'X' 和 'O'(字母 O)。
找到所有被 'X' 围绕的区域,并将这些区域里所有的 'O' 用 'X' 填充。
示例:
X X X X
X O O X
X X O X
X O X X
运行你的函数后,矩阵变为:
X X X X
X X X X
X X X X
X O X X
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/surrounded-regions
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
*/
func solve(board [][]byte) {
for i := 0; i < len(board); i ++ {
for j := 0; j < len(board[i]); j ++ {
if i != 0 && j != 0 && i != len(board) - 1 && j != len(board[i]) - 1 {
continue
}
if board[i][j] == 'O' {
mark(board, i, j)
}
}
}
for i := 0; i < len(board); i ++ {
for j := 0; j < len(board[i]); j ++ {
if board[i][j] == 'O' {
board[i][j] = 'X'
} else if board[i][j] == 'M' {
board[i][j] = 'O'
}
}
}
//for i := 0; i < len(board); i ++ {
// for j := 0; j < len(board[i]); j ++ {
// fmt.Print(string(board[i][j]))
// }
// fmt.Println()
//}
}
func mark(board [][]byte, i int, j int) {
board[i][j] = 'M'//标记
//向左
if j - 1 >= 0 && board[i][j - 1] == 'O' {
mark(board, i, j - 1)
}
//向右
if j + 1 < len(board[i]) && board[i][j + 1] == 'O' {
mark(board, i, j + 1)
}
//向前
if i - 1 >= 0 && board[i - 1][j] == 'O' {
mark(board, i - 1, j)
}
//向后
if i + 1 < len(board) && board[i + 1][j] == 'O' {
mark(board, i + 1, j)
}
}