-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution130.java
More file actions
executable file
·38 lines (37 loc) · 1.44 KB
/
Copy pathSolution130.java
File metadata and controls
executable file
·38 lines (37 loc) · 1.44 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
class Solution130 {
public void solve(char[][] board) {
boolean[][] visited = new boolean[board.length][board[0].length];
for (int i = 0; i < board.length; i++)
for (int j = 0; j < board[0].length; j++)
if (board[i][j] == 'O' && (i == 0 || i == board.length - 1 || j == 0 || j == board[0].length - 1)) {
visited[i][j] = true;
dfs(visited, board, i ,j);
}
for (int i = 0; i < board.length; i++)
for (int j = 0; j < board[0].length; j++)
if (!visited[i][j])
board[i][j] = 'X';
}
public void dfs(boolean[][] visited, char[][] board, int x, int y) {
// 下移
if (x + 1 < board.length && board[x + 1][y] == 'O' && !visited[x + 1][y]) {
visited[x + 1][y] = true;
dfs(visited, board, x + 1, y);
}
// 右移
if (y + 1 < board[0].length && board[x][y+ 1] == 'O' && !visited[x][y + 1]) {
visited[x][y + 1] = true;
dfs(visited, board, x, y + 1);
}
// 上移
if (x - 1 >= 0 && board[x - 1][y] == 'O' && !visited[x - 1][y]) {
visited[x - 1][y] = true;
dfs(visited, board, x - 1, y);
}
// 左移
if (y - 1 >= 0 && board[x][y - 1] == 'O' && !visited[x][y - 1]) {
visited[x][y - 1] = true;
dfs(visited, board, x, y - 1);
}
}
}