-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
1905-count-sub-islands.java
42 lines (37 loc) · 1.34 KB
/
1905-count-sub-islands.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
class Solution {
class RecursiveBiFunction<A, B, C> {
BiFunction<A, B, C> func;
}
public int countSubIslands(final int[][] grid1, final int[][] grid2) {
final int ROWS = grid1.length, COLS = grid1[0].length;
final Set<Integer> visit = new HashSet<>();
final RecursiveBiFunction<Integer, Integer, Boolean> dfs = new RecursiveBiFunction<>();
dfs.func = (r, c) -> {
int flatCoord = r*COLS + c;
if(
r < 0
|| c < 0
|| r == ROWS
|| c == COLS
|| grid2[r][c] == 0
|| visit.contains(flatCoord)
)
return true;
visit.add(flatCoord);
boolean res = true;
if(grid1[r][c] == 0)
res = false;
res = dfs.func.apply(r - 1, c) && res;
res = dfs.func.apply(r + 1, c) && res;
res = dfs.func.apply(r, c - 1) && res;
res = dfs.func.apply(r, c + 1) && res;
return res;
};
int count = 0;
for(int r = 0; r < ROWS; r++)
for(int c = 0; c < COLS; c++)
if(grid2[r][c] != 0 && !visit.contains(r*COLS + c) && dfs.func.apply(r, c))
count += 1;
return count;
}
}