-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path130SurroundedRegions.java
More file actions
75 lines (65 loc) · 2.67 KB
/
130SurroundedRegions.java
File metadata and controls
75 lines (65 loc) · 2.67 KB
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
//https://leetcode.com/problems/surrounded-regions/
class Solution {
void checkRow(int rows, int cols, int colIndex, char[][] board, boolean[][] visited) {
for(int i = 0; i < rows; i++) {
if(board[i][colIndex] == 'O' && !visited[i][colIndex]) {
//dfs(i, colIndex, rows, cols, board, visited);
bfs(i, colIndex, rows, cols, board, visited);
}
}
}
void checkCol(int rows, int cols, int rowIndex, char[][] board, boolean[][] visited) {
for(int j = 0; j < cols; j++) {
if(board[rowIndex][j] == 'O' && !visited[rowIndex][j]) {
//dfs(rowIndex, j, rows, cols, board, visited);
bfs(rowIndex, j, rows, cols, board, visited);
}
}
}
public void solve(char[][] board) {
int rows = board.length, cols = board[0].length;
boolean[][] visited = new boolean[rows][cols];
checkRow(rows, cols, 0, board, visited);
checkRow(rows, cols, cols - 1, board, visited);
checkCol(rows, cols, 0, board, visited);
checkCol(rows, cols, rows - 1, board, visited);
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
if(board[i][j] == 'O' && !visited[i][j]) {
board[i][j] = 'X';
}
}
}
}
void bfs(int i, int j, int rows, int cols, char[][] board, boolean[][] visited) {
int[][] directions = new int[][]{
new int[]{0, 1},
new int[]{0, -1},
new int[]{-1, 0},
new int[]{1, 0}
};
Queue<Integer[]> queue = new LinkedList();
queue.offer(new Integer[]{i, j});
visited[i][j] = true;
while(!queue.isEmpty()) {
Integer[] top = queue.poll();
for(int k = 0; k < directions.length; k++) {
int x = top[0] + directions[k][0], y = top[1] + directions[k][1];
if(x >= 0 && x < rows && y >= 0 && y < cols && !visited[x][y] && board[x][y] == 'O') {
queue.offer(new Integer[]{x, y});
visited[x][y] = true;
}
}
}
}
void dfs(int i, int j, int rows, int cols, char[][] board, boolean[][] visited) {
if(i < 0 || i >= rows || j < 0 || j >= cols || visited[i][j] || board[i][j] == 'X') {
return;
}
visited[i][j] = true;
dfs(i + 1, j, rows, cols, board, visited);
dfs(i - 1, j, rows, cols, board, visited);
dfs(i, j + 1, rows, cols, board, visited);
dfs(i, j - 1, rows, cols, board, visited);
}
}