-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.java
53 lines (48 loc) · 1.59 KB
/
test.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
import java.util.*;
import java.lang.*;
class Test {
public static void main(String[] args) {
Solution obj = new Solution();
int[][] grid = { { 1, 1, 0, 1, 1 },
{ 1, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 1 },
{ 1, 1, 0, 1, 1 }
};
// Function Call
System.out.println("Number of distinct islands is " + obj.countDistinctIslands(grid));
}
}
class Solution {
// Function to find the number of islands.
static int[][] dirs = {{-1,0},{0,1}, {1,0}, {0,-1}};
private static void dfs(int[][] grid, int i, int j, int si, int sj, ArrayList<String> coords) {
if(i<0 || j<0 || i>=grid.length || j>=grid[0].length || grid[i][j] <= 0) {
return;
}
grid[i][j] = -1;
coords.add(Integer.toString(i-si) + " " + Integer.toString(j-sj));
for(int k=0;k<4;k++)
{
dfs(grid, i+dirs[k][0], j+dirs[k][1], si, sj, coords);
}
}
public int countDistinctIslands(int[][] grid) {
// Code here
HashSet<ArrayList<String>> islands = new HashSet<>();
ArrayList<String> v;
for(int i=0;i<grid.length;i++)
{
for(int j=0;j<grid[0].length;j++) {
if(grid[i][j] == 1){
v = new ArrayList<>();
dfs(grid, i, j, i, j, v);
System.out.println(v);
islands.add(v);
}
}
}
Math.max(0, 0)
System.out.println(islands);
return islands.size();
}
}