Poison

200. Number of Islands

DFS
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
class Solution {
private static final int[][] DIRECTIONS = new int[][]{{1, 0}, {-1, 0}, {0, 1}, {0, -1}};

public int numIslands(char[][] grid) {
int m = grid.length, n = grid[0].length;

int landCount = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (grid[i][j] == '1') {
dfs(grid, m, n, i, j);
landCount++;
}
}
}

return landCount;
}

private void dfs(char[][] grid, int m, int n, int i, int j) {
grid[i][j] = '0';
for (int[] direction : DIRECTIONS) {
int nextI = i + direction[0];
int nextJ = j + direction[1];
if (nextI >= 0 && nextI < m && nextJ >= 0 && nextJ < n && grid[nextI][nextJ] == '1') {
dfs(grid, m, n, nextI, nextJ);
}
}
}
}
BFS
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
class Solution {
private static final int[][] DIRECTIONS = new int[][]{{0, 1}, {0, -1}, {1, 0}, {-1, 0}};

public int numIslands(char[][] grid) {
int m = grid.length, n = grid[0].length;

int landCount = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (grid[i][j] == '1') {
bfs(grid, m, n, i, j);
landCount++;
}
}
}

return landCount;
}

private void bfs(char[][] grid, int m, int n, int i, int j) {
Queue<int[]> queue = new LinkedList<>(); // 队列中的点为已经清除了陆地状态的点
grid[i][j] = '0'; // 注意在入队前标记以防止重复入队
queue.offer(new int[]{i, j});

while (!queue.isEmpty()) {
int[] point = queue.poll();

for (int[] direction : DIRECTIONS) {
int nextI = point[0] + direction[0];
int nextJ = point[1] + direction[1];

if (nextI < 0 || nextI >= m || nextJ < 0 || nextJ >= n || grid[nextI][nextJ] == '0') {
continue;
}

grid[nextI][nextJ] = '0'; // 注意在入队前标记以防止重复入队
queue.offer(new int[]{nextI, nextJ});
}
}
}
}

注意入队前标记。

UnionFind
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
class Solution {
private static class UnionFind {

private final int[] parent;
private int areaCount;

public UnionFind(int count) {
this.parent = new int[count];
// 不要忘记初始化每个索引指向自己
for (int i = 0; i < count; i++) {
this.parent[i] = i;
}
this.areaCount = count;
}

public void union(int x, int y) {
int xRoot = getRoot(x);
int yRoot = getRoot(y);
if (xRoot == yRoot) {
return;
}

parent[xRoot] = yRoot; // 注意此处是把 x 的根节点指向 y 的根节点,而不能把 x 指向 y 的根节点,否则 x 的父节点等的指向未被改变
areaCount--;
}

public int getRoot(int x) {
while (x != parent[x]) {
parent[x] = parent[parent[x]]; // path compression
x = parent[x];
}
return x;
}

}

public int numIslands(char[][] grid) {
int m = grid.length, n = grid[0].length;

int[][] directions = new int[][]{{1, 0}, {0, 1}};
UnionFind unionFind = new UnionFind(m * n);

int water = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (grid[i][j] == '1') {
for (int[] direction : directions) {
int nextI = i + direction[0];
int nextJ = j + direction[1];
if (nextI < m && nextJ < n && grid[nextI][nextJ] == '1') {
unionFind.union(getIndex(n, i, j), getIndex(n, nextI, nextJ));
}
}
} else {
water++;
}
}
}

return unionFind.areaCount - water;
}

private int getIndex(int width, int i, int j) {
return i * width + j;
}
}
Reference

200. Number of Islands