forked from DengWangBao/Leetcode-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSurroundedRegions.java
92 lines (84 loc) · 2.71 KB
/
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import java.util.LinkedList;
import java.util.Queue;
public class SurroundedRegions {
/**
* BFS法
* 给与边界的O相邻的所有O点都标为+,然后剩下的O肯定是不与边界O相邻的,则必然是被X包围的,
* 将这些O标为X后,再给剩下的+都还原为O即可
*/
public void solve2(char[][] board) {
if (board.length == 0) {
return;
}
int row = board.length, col = board[0].length;
Queue<int[]> queue = new LinkedList<int[]>();
for (int i = 0; i < col; i++) {
enqueue(board, 0, i, queue);
enqueue(board, row - 1, i, queue);
}
for (int i = 0; i < row; i++) {
enqueue(board, i, 0, queue);
enqueue(board, i, col - 1, queue);
}
while (!queue.isEmpty()) {
int[] pos = queue.poll();
int x = pos[0], y = pos[1];
enqueue(board, x - 1, y, queue);
enqueue(board, x + 1, y, queue);
enqueue(board, x, y + 1, queue);
enqueue(board, x, y - 1, queue);
}
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (board[i][j] == 'O') {
board[i][j] = 'X';
} else if (board[i][j] == '+') {
board[i][j] = 'O';
}
}
}
}
private void enqueue(char[][] board, int i, int j, Queue<int[]> queue) {
if (i >= 0 && i < board.length && j >= 0 && j < board[0].length && board[i][j] == 'O') {
board[i][j] = '+';
queue.add(new int[]{i, j});
}
}
/**
* DFS法
* 数据量大时可能Stack Overflow
*/
public void solve3(char[][] board) {
if (board.length == 0) {
return;
}
int row = board.length, col = board[0].length;
for (int i = 0; i < col; i++) {
dfs(board, 0, i);
dfs(board, row - 1, i);
}
for (int i = 0; i < row; i++) {
dfs(board, i, 0);
dfs(board, i, col - 1);
}
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (board[i][j] == 'O') {
board[i][j] = 'X';
} else if (board[i][j] == '+') {
board[i][j] = 'O';
}
}
}
}
private void dfs(char[][] board, int i, int j) {
if (i < 0 || i >= board.length || j < 0 || j >= board[0].length || board[i][j] != 'O') {
return;
}
board[i][j] = '+';
dfs(board, i - 1, j);
dfs(board, i + 1, j);
dfs(board, i, j + 1);
dfs(board, i, j - 1);
}
}