-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLC_130
More file actions
54 lines (43 loc) · 1.26 KB
/
Copy pathLC_130
File metadata and controls
54 lines (43 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
47
48
49
50
51
52
53
54
class Solution {
public void solve(char[][] board) {
int rows = board.length;
int cols = board[0].length;
for(int i = 0; i < rows; i++){
if(board[i][0] == 'O')
dfs(board, i, 0);
}
for(int i = 0; i < rows; i++){
if(board[i][cols - 1] == 'O')
dfs(board, i, cols - 1);
}
for(int j = 0; j < cols; j++){
if(board[0][j] == 'O')
dfs(board, 0, j);
}
for(int j = 0; j < cols; j++){
if(board[rows - 1][j] == 'O')
dfs(board, rows - 1, j);
}
for(int i = 0; i < rows; i++){
for(int j = 0; j < cols; j++){
if(board[i][j] == 'O')
board[i][j] = 'X';
else if(board[i][j] == 'S')
board[i][j] = 'O';
}
}
}
private void dfs(char[][] board, int row, int col){
if(row < 0 ||
row >= board.length ||
col < 0 ||
col >= board[0].length ||
board[row][col] != 'O')
return;
board[row][col] = 'S';
dfs(board,row+1,col);
dfs(board,row-1,col);
dfs(board,row,col+1);
dfs(board,row,col-1);
}
}