-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay-17 Surrounded Regions
More file actions
46 lines (41 loc) · 1.26 KB
/
Day-17 Surrounded Regions
File metadata and controls
46 lines (41 loc) · 1.26 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
class Solution {
public void solve(char[][] board) {
if(board.length==0) return;
for(int i=0;i<board.length;i++){
if(board[i][0]=='O'){
dfs(board,i,0);
}
if(board[i][board[0].length-1]=='O'){
dfs(board,i,board[0].length-1);
}
}
for(int i=0;i<board[0].length;i++){
if(board[0][i]=='O'){
dfs(board,0,i);
}
if(board[board.length-1][i]=='O'){
dfs(board,board.length-1,i);
}
}
for(int i=0;i<board.length;i++){
for(int j=0;j<board[0].length;j++){
if(board[i][j]=='O'){
board[i][j]='X';
}
else if(board[i][j]=='2'){
board[i][j]='O';
}
}
}
}
void dfs(char[][] board,int row,int col){
if(row<0 || col<0 || row>=board.length || col>=board[0].length || board[row][col]=='X' || board[row][col]=='2'){
return;
}
board[row][col]='2';
dfs(board,row-1,col);
dfs(board,row,col-1);
dfs(board,row+1,col);
dfs(board,row,col+1);
}
}