-
Notifications
You must be signed in to change notification settings - Fork 118
/
SurroundRegions.java
93 lines (75 loc) · 2.39 KB
/
SurroundRegions.java
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
package Algorithms;
import java.util.LinkedList;
import java.util.Queue;
public class SurroundRegions {
public static void main(String[] args) {
char[][] board = {
{'O', 'X', 'X'},
{'X', 'O', 'X'},
{'X', 'X', 'X'},
};
solve(board);
for(int i = 0; i < board.length; i++) {
for (int j = 0; j < board[0].length; j++) {
System.out.print(board[i][j]);
}
System.out.println();
}
}
public static void solve(char[][] board) {
if (board == null) {
return;
}
int row = board.length;
int col = board[0].length;
// BFS the first line and the last line.
for (int i = 0; i < col; i++) {
bfs(board, 0, i);
bfs(board, row - 1, i);
}
// BFS the left line and the right line.
for (int i = 1; i < row - 1; i++) {
bfs(board, i, 0);
bfs(board, i, col - 1);
}
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (board[i][j] == 'B') {
board[i][j] = 'O';
} else if (board[i][j] == 'O') {
board[i][j] = 'X';
}
}
}
return;
}
public static void bfs(char[][] board, int x, int y) {
Queue<Integer> q = new LinkedList<Integer>();
// 把x, y放入queue代表准备要访问它的邻接点
visit(board, x, y, q);
while (!q.isEmpty()) {
int num = q.poll();
int r = num / board[0].length;
int c = num % board[0].length;
// visit the surrounded points.
visit(board, r + 1, c, q);
visit(board, r - 1, c, q);
visit(board, r, c + 1, q);
visit(board, r, c - 1, q);
}
return;
}
public static void visit(char[][] board, int x, int y, Queue<Integer> q) {
int row = board.length;
int col = board[0].length;
if (x < 0 || x >= row || y < 0 || y >= col) {
return;
}
if (board[x][y] != 'O') {
return;
}
board[x][y] = 'B';
q.offer(col * x + y);
return;
}
}