-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0130-surrounded-regions.java
More file actions
40 lines (39 loc) · 1.34 KB
/
0130-surrounded-regions.java
File metadata and controls
40 lines (39 loc) · 1.34 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
class Solution {
public void solve(char[][] board) {
boolean [][] visited = new boolean[board.length + 1][board[0].length + 1];
for (int i=0; i<board.length; i++) {
for (int j=0; j<board[0].length; j++) {
if (board[i][j] == 'X') {
visited[i][j] = true;
}
if (i == 0 || j == 0 || i == board.length - 1
|| j == board[0].length-1) {
if (board[i][j] == 'O') {
dfs(board, i, j, visited);
}
}
}
}
for (int i=0; i<board.length; i++) {
for (int j=0; j<board[0].length; j++) {
if (board[i][j] == 'O' && !visited[i][j]) {
board[i][j] = 'X';
}
}
}
}
//check which cells shouldnt be painted and mark them as visited
public void dfs(char[][] board, int i, int j, boolean visited[][]) {
if (i<0 || j<0 || i>=board.length || j>=board[0].length || visited[i][j]) {
return;
}
if (board[i][j] == 'X') {
return;
}
visited[i][j] = true;
dfs(board, i+1, j, visited);
dfs(board, i-1, j, visited);
dfs(board, i, j+1, visited);
dfs(board, i, j-1, visited);
}
}