forked from algorithm009-class01/algorithm009-class01
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolveI.java
More file actions
36 lines (33 loc) · 1.12 KB
/
SolveI.java
File metadata and controls
36 lines (33 loc) · 1.12 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
package tree;
public class SolveI {
public void solve(char[][] board){
if (board == null || board.length == 0) return;
int rows = board.length;
int cols = board[0].length;
for (int i = 0; i < rows; i++) {
if (board[i][0] == 'O') dfs(i,1,board);
if (board[i][cols-1] =='O') dfs(i,cols-2,board);
}
for (int i=0;i<cols;i++){
if (board[0][i] == 'O')
dfs(1,i,board);
if (board[rows-1][i] == 'O')
dfs(rows-2,i,board);
}
for (int i = 1; i < rows-1; i++) {
for (int j = 1; j < cols-1; j++) {
if (board[i][j] == '*') board[i][j] ='O';
else if(board[i][j] =='O') board[i][j] ='X';
}
}
}
private void dfs(int i, int j, char[][] board) {
if (i<=0 || j<=0 || i>= board.length-1 || j>=board[0].length-1 || board[i][j] =='X') return;
if (board[i][j] == '*') return;
if (board[i][j] =='O') board[i][j] ='*';
dfs(i+1,j,board);
dfs(i-1,j,board);
dfs(i,j+1,board);
dfs(i,j-1,board);
}
}