[LeetCode] Number of Distinct Islands II

711. Number of Distinct Islands II

You are given an m x n binary matrix grid. 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.

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).

Return the number of distinct islands.

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
class Solution {
unordered_set<string> vis[51][51];
vector<vector<int>> bfs(vector<vector<int>>& g, int sy, int sx, int n, int m) {
vector<vector<int>> copy(n, vector<int>(m, 0));
int t = sy, b = sy, l = sx, r = sx;
int dy[4]{-1,0,1,0}, dx[4]{0,1,0,-1};
queue<pair<int,int>> q;
q.push({sy,sx});
g[sy][sx] = 0;
copy[sy][sx] = 1;

while(!q.empty()) {
auto [y, x] = q.front(); q.pop();
for(int i = 0; i < 4; i++) {
int ny = y + dy[i], nx = x + dx[i];
if(0 <= ny and ny < n and 0 <= nx and nx < m and g[ny][nx]) {
g[ny][nx] = 0;
copy[ny][nx] = 1;
t = max(t, ny); b = min(b, ny);
r = max(r, nx); l = min(l, nx);
q.push({ny,nx});
}
}
}


vector<vector<int>> shape(t - b + 1, vector<int>(r - l + 1, 0));
for(int i = b; i <= t; i++) {
for(int j = l; j <= r; j++) {
shape[i-b][j-l] = copy[i][j];
}
}
return shape;
}
bool insert(vector<vector<int>> shape) {
string s1 = "", s2 = "", s3 = "", s4 = "", s5 = "", s6 = "";
int n = shape.size(), m = shape[0].size();
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
s1 += to_string(shape[i][j]);
}
}
for(int j = 0; j < m; j++) {
for(int i = n - 1; i >= 0; i--) {
s2 += to_string(shape[i][j]);
}
}
for(int i = n - 1; i >= 0; i--) {
for(int j = m - 1; j >= 0; j--) {
s3 += to_string(shape[i][j]);
}
}
for(int j = m - 1; j >= 0; j--) {
for(int i = 0; i < n; i++) {
s4 += to_string(shape[i][j]);
}
}
for(int i = n - 1; i >= 0; i--) {
for(int j = 0; j < m; j++) {
s5 += to_string(shape[i][j]);
}
}
for(int i = 0; i < n; i++) {
for(int j = m - 1; j >= 0; j--) {
s6 += to_string(shape[i][j]);
}
}
bool success = true;
success &= !vis[n][m].count(s1);
success &= !vis[m][n].count(s2);
success &= !vis[n][m].count(s3);
success &= !vis[m][n].count(s4);
success &= !vis[n][m].count(s5);
success &= !vis[n][m].count(s6);

vis[n][m].insert(s1);
vis[m][n].insert(s2);
vis[n][m].insert(s3);
vis[m][n].insert(s4);
vis[n][m].insert(s5);
vis[n][m].insert(s6);

return success;
}
public:
int numDistinctIslands2(vector<vector<int>>& g) {
int n = g.size(), m = g[0].size(), res = 0;
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
if(g[i][j]) {
auto shape = bfs(g, i, j, n, m);
res += insert(shape);
}
}
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/05/02/PS/LeetCode/number-of-distinct-islands-ii/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.