-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathsurrounded_regions.java
59 lines (57 loc) · 1.95 KB
/
surrounded_regions.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
class Solution {
public void solve(char[][] board) {
if (board == null || board.length == 0)
return;
int row = board.length;
int col = board[0].length;
Queue<int[]> queue = new LinkedList<>();
// add all the edge on top left right botom to queue
for (int i=0; i<row; i++) {
if (board[i][0] == 'O') {
board[i][0] = '+';
queue.offer(new int []{i, 0});
}
if (board[i][col - 1] == 'O') {
board[i][col - 1] = '+';
queue.offer(new int []{i, col - 1});
}
}
for (int i=0; i<col; i++) {
if (board[0][i] == 'O') {
board[0][i] = '+';
queue.offer(new int [] {0, i});
}
if (board[row - 1][i] == 'O') {
board[row - 1][i] = '+';
queue.offer(new int [] {row - 1, i});
}
}
bfs(queue, row, col, board);
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 bfs(Queue<int[]> queue, int row, int col, char [][] board) {
final int [][] directions = new int [][]{{0, 1}, {1, 0}, {-1, 0}, {0, -1}};
while (!queue.isEmpty()) {
int [] currentPos = queue.poll();
int x = currentPos[0];
int y = currentPos[1];
for (int [] dir : directions) {
int newX = x + dir[0];
int newY = y + dir[1];
if (newX < 0 || newY < 0 || newX >= row || newY >= col || board[newX][newY] != 'O')
continue;
board[newX][newY] = '+';
queue.offer(new int [] {newX, newY});
}
}
}
}