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

Example 1:

``````11000
10000
00001
00011
``````

Given the above grid map, return `1`

Notice that:

``````11
1
``````

and

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

Example 2:

``````11100
10001
01001
01110
``````

Given the above grid map, return `2`.

Here are the two distinct islands:

``````111
1
``````

and

``````1
1
``````

Notice that:

``````111
1
``````

and

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

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

``````class Solution {
public:
int numDistinctIslands2(vector<vector<int>>& grid) {
int m = grid.size(), n = grid[0].size();
set<vector<pair<int, int>>> st;
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (grid[i][j]) {
vector<pair<int, int>> shape;
helper(grid, i, j, shape);
st.insert(normalize(shape));
}
}
}
return st.size();
}
void helper(vector<vector<int>>& grid, int x, int y, vector<pair<int, int>>& shape) {
if (x < 0 || x >= grid.size() || y < 0 || y >= grid[0].size()) return;
if (grid[x][y] == 0) return;
grid[x][y] = 0;
shape.push_back({x, y});
helper(grid, x + 1, y, shape);
helper(grid, x - 1, y, shape);
helper(grid, x, y + 1, shape);
helper(grid, x, y - 1, shape);
}
vector<pair<int, int>> normalize(vector<pair<int, int>>& shape) {
vector<vector<pair<int, int>>> shapes(8);
for (auto &a : shape) {
int x = a.first, y = a.second;
shapes[0].push_back({x, y});
shapes[1].push_back({x, -y});
shapes[2].push_back({-x, y});
shapes[3].push_back({-x, -y});
shapes[4].push_back({y, x});
shapes[5].push_back({y, -x});
shapes[6].push_back({-y, x});
shapes[7].push_back({-y, -x});
}
for (auto &a : shapes) {
sort(a.begin(), a.end());
for (int i = (int)shape.size() - 1; i >= 0; --i) {
a[i].first -= a[0].first;
a[i].second -= a[0].second;
}
}
sort(shapes.begin(), shapes.end());
return shapes[0];
}
};
``````

``````class Solution {
public:
int numDistinctIslands2(vector<vector<int>>& grid) {
int m = grid.size(), n = grid[0].size();
set<vector<pair<int, int>>> st;
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (grid[i][j] == 0) continue;
grid[i][j] = 0;
vector<pair<int, int>> shape{{i, j}};
int i = 0;
while (i < shape.size()) {
auto t = shape[i++];
int x = t.first, y = t.second;
if (x > 0 && grid[x - 1][y] != 0) {
grid[x - 1][y] = 0;
shape.push_back({x - 1, y});
}
if (x + 1 < m && grid[x + 1][y] != 0) {
grid[x + 1][y] = 0;
shape.push_back({x + 1, y});
}
if (y > 0 && grid[x][y - 1] != 0) {
grid[x][y - 1] = 0;
shape.push_back({x, y - 1});
}
if (y + 1 < n && grid[x][y + 1] != 0) {
grid[x][y + 1] = 0;
shape.push_back({x, y + 1});
}
}
st.insert(normalize(shape));
}
}
return st.size();
}
vector<pair<int, int>> normalize(vector<pair<int, int>>& shape) {
vector<vector<pair<int, int>>> shapes(8);
for (auto &a : shape) {
int x = a.first, y = a.second;
shapes[0].push_back({x, y});
shapes[1].push_back({x, -y});
shapes[2].push_back({-x, y});
shapes[3].push_back({-x, -y});
shapes[4].push_back({y, x});
shapes[5].push_back({y, -x});
shapes[6].push_back({-y, x});
shapes[7].push_back({-y, -x});
}
for (auto &a : shapes) {
sort(a.begin(), a.end());
for (int i = (int)shape.size() - 1; i >= 0; --i) {
a[i].first -= a[0].first;
a[i].second -= a[0].second;
}
}
sort(shapes.begin(), shapes.end());
return shapes[0];
}
};
``````

Github 同步地址：

https://github.com/grandyang/leetcode/issues/711

Number of Distinct Islands

https://leetcode.com/problems/number-of-distinct-islands-ii/

https://leetcode.com/problems/number-of-distinct-islands-ii/discuss/231300/C%2B%2B-BFSDFS

https://leetcode.com/problems/number-of-distinct-islands-ii/discuss/108794/Consise-C%2B%2B-solution-using-DFS-%2Bsorting-to-find-canonical-representation-for-each-island

LeetCode All in One 题目讲解汇总(持续更新中…)

 微信打赏 Venmo 打赏
（欢迎加入博主的知识星球，博主将及时答疑解惑，并分享刷题经验与总结，试运营期间前五十位可享受半价优惠～）

×

Help us with donation