-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountIsland.java
More file actions
48 lines (42 loc) · 1.34 KB
/
CountIsland.java
File metadata and controls
48 lines (42 loc) · 1.34 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
package ArraysD2;
public class CountIsland {
static void DFS(int[][] M, int i, int j, int ROW, int COL) {
if (i < 0 || j < 0 || i > (ROW - 1) || j > (COL - 1) || M[i][j] != 1) {
return;
}
if (M[i][j] == 1) {
M[i][j] = 0;
DFS(M, i + 1, j, ROW, COL);
DFS(M, i - 1, j, ROW, COL);
DFS(M, i, j + 1, ROW, COL);
DFS(M, i, j - 1, ROW, COL);
DFS(M, i + 1, j + 1, ROW, COL);
DFS(M, i - 1, j - 1, ROW, COL);
DFS(M, i + 1, j - 1, ROW, COL);
DFS(M, i - 1, j + 1, ROW, COL);
}
}
static int countIslands(int[][] M) {
int ROW = M.length;
int COL = M[0].length;
int count = 0;
for (int i = 0; i < ROW; i++) {
for (int j = 0; j < COL; j++) {
if (M[i][j] == 1) {
count++;
DFS(M, i, j, ROW, COL);
}
}
}
return count;
}
// Time Complexity - O(R*C), Auxiliary Space - O(R*C)
public static void main(String[] args) {
int[][] M = {{1, 1, 0, 0, 0},
{0, 1, 0, 0, 1},
{1, 0, 0, 1, 1},
{0, 0, 0, 0, 0},
{1, 0, 1, 0, 1}};
System.out.print("Number of islands is: " + countIslands(M));
}
}