[LintCode] Problem 804 - Number of Distinct Islands II

Given a non-empty 2D array grid of 0’s and 1’s, an island is a group of 1’s (representing land) connected 4-directionally (horizontal or vertical.) You may assume all four edges of the grid are surrounded by water.

Count the number of distinct islands. An island is considered to be the same as another if they have the same shape, or have the same shape after rotation (90, 180, or 270 degrees only) or reflection (left/right direction or up/down direction).

Note

The length of each dimension in the given grid does not exceed 50.

Example

No.1

Input: [[1,1,0,0,0],[1,0,0,0,0],[0,0,0,0,1],[0,0,0,1,1]]

Output: 1

Explanation:
The island is look like this:

1
2
3
4
11000
10000
00001
00011

Notice that:

1
2
11
1

and

1
2
 1
11

are considered same island shapes. Because if we make a 180 degrees clockwise rotation on the first island, then two islands will have the same shapes.

No.2

Input: [[1,1,1,0,0],[1,0,0,0,1],[0,1,0,0,1],[0,1,1,1,0]]

Output: 2

Explanation:
The island is look like this:

1
2
3
4
11100
10001
01001
01110

Here are the two distinct islands:

1
2
111
1

and

1
2
1
1

Notice that:

1
2
111
1

and

1
2
1
111

are considered same island shapes. Because if we flip the first array in the up/down direction, then they have the same shapes.

Code

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
private int[][] dirs = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
private int[][] trans = {{1, 1}, {-1, 1}, {1, -1}, {-1, -1}};
private Comparator<int[]> comparator = new Comparator<int[]>(){
@Override
public int compare(int[] o1, int[] o2) {
return o1[0] == o2[0] ? o1[1] - o2[1] : o1[0] - o2[0];
}
};

public int numDistinctIslands2(int[][] grid) {
int m = grid.length;
int n = grid[0].length;
Set<String> set = new HashSet<>();
boolean[][] visit = new boolean[m][n];

for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (grid[i][j] == 1 && !visit[i][j]) {
List<int[]> island = new ArrayList<>();
dfs(island, grid, visit, m, n, i, j);
set.add(normalize(island));
}
}
}

return set.size();
}

private void dfs(List<int[]> island, int[][] grid, boolean[][] visit, int m, int n, int x, int y) {
island.add(new int[] {x, y});
visit[x][y] = true;

for (int[] dir : dirs) {
int newX = x + dir[0];
int newY = y + dir[1];

if (newX < 0 || newY < 0 || newX >= m || newY >= n || grid[newX][newY] == 0 || visit[newX][newY])
continue;

dfs(island, grid, visit, m, n, newX, newY);
}
}

private String normalize(List<int[]> island) {
List<int[]>[] shapes = new ArrayList[8];
String[] results = new String[8];

for (int i = 0; i < trans.length; i++) {
int[] tran = trans[i];
shapes[i] = new ArrayList<>();
shapes[i + 4] = new ArrayList<>();

for (int[] point : island) {
shapes[i].add(new int[] {point[0] * tran[0], point[1] * tran[1]});
shapes[i + 4].add(new int[] {point[1] * tran[1], point[0] * tran[0]});
}

Collections.sort(shapes[i], comparator);
Collections.sort(shapes[i + 4], comparator);
}

for (int i = 0; i < shapes.length; i++) {
StringBuilder sb = new StringBuilder();
int originX = shapes[i].get(0)[0];
int originY = shapes[i].get(0)[1];

for (int[] point : shapes[i])
sb.append(point[0] - originX).append(",").append(point[1] - originY).append(",");

results[i] = sb.toString();
}

Arrays.sort(results);
return results[0];
}