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 and only if one island can be translated (and not rotated or reflected) to equal the other.

Example 1:

11000
11000
00011
00011

Given the above grid map, return 1.

Example 2:

11011
10000
00001
11011

Given the above grid map, return 3.

Notice that:

11
1

and

 1
11

are considered different island shapes, because we do not consider reflection / rotation.

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

这道题让我们求不同岛屿的个数,是之前那道 Number of Islands 的拓展,难点是如何去判断两个岛屿是否是不同的岛屿,首先1的个数肯定是要相同,但是1的个数相同不能保证一定是相同的岛屿,比如例子2中的那两个岛屿的就不相同,就是说两个相同的岛屿通过平移可以完全重合,但是不能旋转。如何来判断呢,可以发现可以通过相对位置坐标来判断,比如使用岛屿的最左上角的1当作基点,那么基点左边的点就是 (0,-1),右边的点就是 (0,1), 上边的点就是 (-1,0),下面的点就是 (1,0)。则例子1中的两个岛屿都可以表示为 [(0,0), (0,1), (1,0), (1,1)],点的顺序是基点-右边点-下边点-右下点。通过这样就可以判断两个岛屿是否相同了,下面这种解法没有用数组来存,而是 encode 成了字符串,比如这四个点的数组就存为 "0_0_0_1_1_0_1_1_",然后把字符串存入集合 unordered_set 中,利用其自动去重复的特性,就可以得到不同的岛屿的数量啦,参见代码如下:

解法一:

class Solution {
public:
vector<vector<int>> dirs{{,-},{-,},{,},{,}};
int numDistinctIslands(vector<vector<int>>& grid) {
int m = grid.size(), n = grid[].size();
unordered_set<string> res;
vector<vector<bool>> visited(m, vector<bool>(n, false));
for (int i = ; i < m; ++i) {
for (int j = ; j < n; ++j) {
if (grid[i][j] == && !visited[i][j]) {
set<string> s;
helper(grid, i, j, i, j, visited, s);
string t = "";
for (auto str : s) t += str + "_";
res.insert(t);
}
}
}
return res.size();
}
void helper(vector<vector<int>>& grid, int x0, int y0, int i, int j, vector<vector<bool>>& visited, set<string>& s) {
int m = grid.size(), n = grid[].size();
visited[i][j] = true;
for (auto dir : dirs) {
int x = i + dir[], y = j + dir[];
if (x < || x >= m || y < || y >= n || grid[x][y] == || visited[x][y]) continue;
string str = to_string(x - x0) + "_" + to_string(y - y0);
s.insert(str);
helper(grid, x0, y0, x, y, visited, s);
}
}
};

当然我们也可以不 encode 字符串,直接将相对坐标存入数组中,然后把整个数组放到集合 set 中,还是会去掉相同的数组,而且这种解法直接在 grid 数组上标记访问过的位置,写起来更加简洁了,参见代码如下:

解法二:

class Solution {
public:
vector<vector<int>> dirs{{,-},{-,},{,},{,}};
int numDistinctIslands(vector<vector<int>>& grid) {
int m = grid.size(), n = grid[].size();
set<vector<pair<int, int>>> res;
for (int i = ; i < m; ++i) {
for (int j = ; j < n; ++j) {
if (grid[i][j] != ) continue;
vector<pair<int, int>> v;
helper(grid, i, j, i, j, v);
res.insert(v);
}
}
return res.size();
}
void helper(vector<vector<int>>& grid, int x0, int y0, int i, int j, vector<pair<int, int>>& v) {
int m = grid.size(), n = grid[].size();
if (i < || i >= m || j < || j >= n || grid[i][j] <= ) return;
grid[i][j] *= -;
v.push_back({i - x0, j - y0});
for (auto dir : dirs) {
helper(grid, x0, y0, i + dir[], j + dir[], v);
}
}
};

既然递归 DFS 可以,那么迭代的 BFS 就坐不住了,其实思路没什么区别,这种类似迷宫遍历的题都是一个套路,整体框架都很像,细枝末节需要改改就行了,参见代码如下:

解法三:

class Solution {
public:
vector<vector<int>> dirs{{,-},{-,},{,},{,}};
int numDistinctIslands(vector<vector<int>>& grid) {
int m = grid.size(), n = grid[].size();
set<vector<pair<int, int>>> res;
for (int i = ; i < m; ++i) {
for (int j = ; j < n; ++j) {
if (grid[i][j] != ) continue;
vector<pair<int, int>> v;
queue<pair<int, int>> q{{{i, j}}};
grid[i][j] *= -;
while (!q.empty()) {
auto t = q.front(); q.pop();
for (auto dir : dirs) {
int x = t.first + dir[], y = t.second + dir[];
if (x < || x >= m || y < || y >= n || grid[x][y] <= ) continue;
q.push({x, y});
grid[x][y] *= -;
v.push_back({x - i, y - j});
}
}
res.insert(v);
}
}
return res.size();
}
};

Github 同步地址:

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

类似题目:

Number of Islands

Number of Distinct Islands II

参考资料:

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

https://leetcode.com/problems/number-of-distinct-islands/discuss/150037/DFS-with-Explanations

https://leetcode.com/problems/number-of-distinct-islands/discuss/108474/JavaC%2B%2B-Clean-Code

https://leetcode.com/problems/number-of-distinct-islands/discuss/108475/Java-very-Elegant-and-concise-DFS-Solution(Beats-100)

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

[LeetCode] 694. Number of Distinct Islands 不同岛屿的个数的更多相关文章

  1. [LeetCode] Number of Distinct Islands 不同岛屿的个数

    Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) conn ...

  2. [leetcode]694. Number of Distinct Islands你究竟有几个异小岛?

    Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) conn ...

  3. [LeetCode] 694. Number of Distinct Islands

    Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) conn ...

  4. [LeetCode] 711. Number of Distinct Islands II_hard tag: DFS

    Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) conn ...

  5. leetcode 200. Number of Islands 、694 Number of Distinct Islands 、695. Max Area of Island 、130. Surrounded Regions

    两种方式处理已经访问过的节点:一种是用visited存储已经访问过的1:另一种是通过改变原始数值的值,比如将1改成-1,这样小于等于0的都会停止. Number of Islands 用了第一种方式, ...

  6. [LeetCode] 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) conn ...

  7. 694. Number of Distinct Islands 形状不同的岛屿数量

    [抄题]: Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land ...

  8. 【LeetCode】694. Number of Distinct Islands 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 日期 题目地址:https://leetcod ...

  9. [LeetCode] 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) conn ...

随机推荐

  1. git push 报504 (因提交文件内容过大而失败的解决方案)

    Enumerating objects: 60, done. Counting objects: 100% (60/60), done. Delta compression using up to 4 ...

  2. 第一届云原生应用大赛火热报名中! helm install “一键安装”应用触手可及!

    云原生应用,是指符合“云原生”理念的应用开发与交付模式,这是当前在云时代最受欢迎的应用开发最佳实践. 在现今的云原生生态当中,已经有很多成熟的开源软件被制作成了 Helm Charts,使得用户可以非 ...

  3. 【LOJ#3146】[APIO2019]路灯(树套树)

    [LOJ#3146][APIO2019]路灯(树套树) 题面 LOJ 题解 考场上因为\(\text{bridge}\)某个\(\text{subtask}\)没有判\(n=1\)的情况导致我卡了\( ...

  4. 【RT-Thread笔记】IO设备模型及GPIO设备

    RTT内核对象--设备 RT-Thread有多种内核对象,其中设备device就是其中一种. 内核继承关系图如下: 设备继承关系图如下: device对象对应的结构体如下: 其中,设备类型type有如 ...

  5. vue 上传进度显示

    参考资料: https://ask.csdn.net/questions/767017 https://www.cnblogs.com/best-fyx/p/11363506.html 我使用的是el ...

  6. rsync性能终极优化【Optimize rsync performance】

    前言 将文件从一台计算机同步或备份到另一台计算机的快速简便的方法是使用rsync.我将介绍通常用于备份数据的命令行选项,并显示一些选项以极大地将传输速度从大约20-25 MB / s加快到90 MB ...

  7. 数据解析模块BeautifulSoup简单使用

    一.准备环境: 1.准备测试页面test.html <html> <head> <title> The Dormouse's story </title> ...

  8. BootStrap-treeview 参考

    简要教程 bootstrap-treeview是一款效果非常酷的基于bootstrap的jQuery多级列表树插件.该jQuery插件基于Twitter Bootstrap,以简单和优雅的方式来显示一 ...

  9. youtube视频在线下载

    youtube视频在线下载网站: https://www.clipconverter.cc/ youtube视频样例: https://www.youtube.com/watch?v=NMkgz0AR ...

  10. ca动画

    //动画上下文-(void)animationOfUIKit{    UIView *redView=[[UIView alloc]initWithFrame:CGRectMake(10, 10, 1 ...