-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path130. Surrounded Regions.java
More file actions
37 lines (32 loc) · 1000 Bytes
/
Copy path130. Surrounded Regions.java
File metadata and controls
37 lines (32 loc) · 1000 Bytes
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
class Solution {
public void solve(char[][] board) {
for(int i = 0; i < board.length; i++)
{
for(int j = 0; j < board[i].length; j++)
{
if(i == 0 || i == board.length-1 || j == 0 || j == board[i].length-1)
dfs(board, i, j);
}
}
for(int i = 0; i < board.length; i++)
{
for(int j = 0; j < board[i].length; j++)
{
if(board[i][j] == 'A')
board[i][j] = 'O';
else if(board[i][j] == 'O')
board[i][j] = 'X';
}
}
}
public void dfs(char board[][], int i, int j)
{
if(i < 0 || i >= board.length || j < 0 || j >= board[i].length || board[i][j] != 'O')
return;
board[i][j] = 'A';
dfs(board, i+1, j);
dfs(board, i-1, j);
dfs(board, i, j+1);
dfs(board, i, j-1);
}
}