-
Notifications
You must be signed in to change notification settings - Fork 0
/
NumberOfIslands.java
56 lines (51 loc) · 1.45 KB
/
NumberOfIslands.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
// https://leetcode.com/problems/number-of-islands/
// #bfs #dfs #dsu
class Solution {
public int numIslands(char[][] grid) {
int rows = grid.length;
if (rows == 0) return 0;
int cols = grid[0].length;
boolean[][] visited = new boolean[rows][cols];
int ret = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (grid[i][j] == '1' && !visited[i][j]) {
ret++;
fill(grid, visited, i, j, rows, cols);
}
}
}
return ret;
}
private static int[] dx = {-1, 0, 0, 1};
private static int[] dy = {0, -1, 1, 0};
private static boolean valid(boolean[][] visited, int x, int y, int rows, int cols) {
if (x < 0 || x >= rows) return false;
if (y < 0 || y >= cols) return false;
if (visited[x][y]) return false;
return true;
}
private class Node {
int x, y;
Node(int x, int y) {
this.x = x;
this.y = y;
}
}
private void fill(char[][] grid, boolean[][] visited, int row, int col, int rows, int cols) {
visited[row][col] = true;
Deque<Node> dq = new LinkedList<>();
dq.add(new Node(row, col));
while (!dq.isEmpty()) {
Node cur = dq.pollFirst();
for (int i = 0; i < dx.length; i++) {
int x = cur.x + dx[i];
int y = cur.y + dy[i];
if (valid(visited, x, y, rows, cols) && grid[x][y] == '1') {
visited[x][y] = true;
dq.addLast(new Node(x, y));
}
}
}
}
}