-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay_17_SurroundedRegions.java
52 lines (38 loc) · 1.2 KB
/
Day_17_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
class Solution {
public void solve(char[][] board) {
if(board.length<3 || board[0].length<3) return;
int m=board.length;
int n=board[0].length;
for(int i=0;i<m;i++){
if(board[i][0]=='O'){
dfs(board,i,0);
}
if(board[i][n-1]=='O'){
dfs(board,i,n-1);
}
}
for(int i=0;i<n;i++){
if(board[0][i]=='O'){
dfs(board,0,i);
}
if(board[m-1][i]=='O'){
dfs(board,m-1,i);
}
}
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(board[i][j]=='O') board[i][j]='X';
if(board[i][j]=='Y') board[i][j]='O';
}
}
}
public void dfs(char[][] board,int i,int j){
if(i>=0 && j>=0 && i<board.length && j<board[i].length && board[i][j]=='O'){
board[i][j]='Y';
dfs(board,i+1,j);
dfs(board,i-1,j);
dfs(board,i,j+1);
dfs(board,i,j-1);
}
}
}