-
Notifications
You must be signed in to change notification settings - Fork 0
/
130_SurroundedRegions.java
59 lines (54 loc) · 1.64 KB
/
130_SurroundedRegions.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
// Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'.
// A region is captured by flipping all 'O's into 'X's in that surrounded region.
// For example,
// X X X X
// X O O X
// X X O X
// X O X X
// After running your function, the board should be:
// X X X X
// X X X X
// X X X X
// X O X X
public class Solution {
Queue<Integer> queue;
public void solve(char[][] board) {
if(board.length < 1) return;
queue = new LinkedList<>();
int row = board.length;
int col = board[0].length;
for(int i=0; i<row; i++) {
enqueue(board, i, 0);
enqueue(board, i, col - 1);
}
for(int j=1; j<col-1; j++) {
enqueue(board, 0, j);
enqueue(board, row - 1, j);
}
while(!queue.isEmpty()) {
int cur = queue.poll();
int x = cur / col;
int y = cur % col;
// if(board[x][y] == 'O') {
// board[x][y] = '#';
// }
enqueue(board, x - 1, y);
enqueue(board, x + 1, y);
enqueue(board, x, y - 1);
enqueue(board, x, y + 1);
}
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (board[i][j] == '#') board[i][j] = 'O';
else if (board[i][j] == 'O') board[i][j] = 'X';
}
}
}
public void enqueue(char[][] board, int x, int y) {
if(x <0 || y < 0 || x >= board.length || y >= board[0].length || board[x][y] != 'O') {
return;
}
queue.offer(x * board[0].length + y);
board[x][y] = '#';
}
}