-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumberofIslands.java
More file actions
126 lines (111 loc) · 3.76 KB
/
Copy pathNumberofIslands.java
File metadata and controls
126 lines (111 loc) · 3.76 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
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/* numIslands problem is different from the word search in matrix problem
* where you need to reset the cells which are marked */
public class Solution {
/**
* @param grid a boolean 2D matrix
* @return an integer
*/
public int numIslands(boolean[][] grid) {
if(grid.length == 0) return 0;
int m = grid.length;
int n = grid[0].length;
int cnt = 0;
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
if(grid[i][j]){
cnt++;
explore(grid, i, j);
}
}
}
return cnt;
}
private void explore(boolean[][] grid, int i, int j){
int m = grid.length;
int n = grid[0].length;
if(i < 0 || i >=m || j < 0 || j>=n || !grid[i][j]) return;
// flip the island into water if we can modify the original matrix
grid[i][j] = false;
explore(grid, i+1, j);
explore(grid, i-1, j);
explore(grid, i, j+1);
explore(grid, i, j-1);
}
}
// treat the problem as a DFS problem.
// In tree DFS, you have root.left, and root.right. Here you have 4 directions, and you do not want visit the visited node again.
// Two ways to achive this. Use a Map or flip the '1' into '0'
// input is char[][] instead of boolean[][]
class Solution {
public int numIslands(char[][] grid) {
if(grid.length == 0) return 0;
int m = grid.length;
int n = grid[0].length;
int cnt = 0;
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
if(grid[i][j] == '1'){
cnt++;
explore(grid, i, j);
}
}
}
return cnt;
}
private void explore(char[][] grid, int i, int j){
int m = grid.length;
int n = grid[0].length;
if(i < 0 || i >=m || j < 0 || j>=n || grid[i][j] == '0') return;
// flip the island into water if we can modify the original matrix
grid[i][j] = '0';
explore(grid, i+1, j);
explore(grid, i-1, j);
explore(grid, i, j+1);
explore(grid, i, j-1);
}
}
// DFS search using stack
class Solution {
public int numIslands(char[][] A) {
if (A == null || A[0].length == 0) return 0;
int cnt = 0;
int m = A.length;
int n = A[0].length;
boolean[][] v = new boolean[m][n];
Deque<int[]> st = new ArrayDeque<>();
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (A[i][j] == '1' && !v[i][j]) {
st.push(new int[]{i, j});
while (!st.isEmpty()) {
int[] x = st.pop();
v[x[0]][x[1]] = true;
for (int[] t : moves(x, v, A)) {
st.push(t);
}
}
cnt++;
}
}
}
return cnt;
}
private List<int[]> moves(int[] p, boolean[][] v, char[][] A) {
int i = p[0];
int j = p[1];
int m = v.length;
int n = v[0].length;
List<int[]> result = new ArrayList<>();
if (legitMove(A, v, i, j+1)) result.add(new int[]{i, j+1});
if (legitMove(A, v, i, j-1)) result.add(new int[]{i, j-1});
if (legitMove(A, v, i+1, j)) result.add(new int[]{i+1, j});
if (legitMove(A, v, i-1, j)) result.add(new int[]{i-1, j});
return result;
}
private boolean legitMove(char[][] A, boolean[][] v, int i, int j) {
int m = A.length;
int n = A[0].length;
if(j >= n || i < 0 || i >= m || j < 0) return false;
return A[i][j] == '1' && !v[i][j];
}
}