【LeetCode】694. Number of Distinct Islands 解题报告 (C++)
- 作者: 负雪明烛
- id: fuxuemingzhu
- 个人博客:http://fuxuemingzhu.cn/
题目地址:https://leetcode-cn.com/problems/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) 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.
题目大意
找出形状不同的岛屿数目。
解题方法
DFS
这个题如果是问岛屿的数量,那么可以直接使用单纯的DFS解决。但是难点在于考察不同
的岛屿。
为了保存每个岛屿的形状,我们使用了vector path,里面存放的是以进入dfs的坐标为起点,把四联通的每个1转化成相对坐标,并且hash运算,保存dfs的路径。只要dfs的查找方式是固定的,那么路径就是一样的,从而得到的小岛的形状也是固定的。
在dfs的搜索中,一边搜索一边把搜索过的位置全部至0,这是个小窍门,即设置已经走过的位置不可用,从而达到防止重复走的问题。
求相对形状:把每个岛的左上角当作是 (0, 0),例如,如果一个岛是由 [(2, 3), (2, 4), (3, 4)] 构成,当固定左上角时,我们可以把这个形状看作是 [(0, 0), (0, 1), (1, 1)]。
C++中可以使用set对vector去重,但是unordered_set是不可以的。
C++代码如下:
class Solution {
public:
int numDistinctIslands(vector<vector<int>>& grid) {
if (!grid.size() || !grid[0].size()) return 0;
M = grid.size();
N = grid[0].size();
set<vector<int>> s;
for (int i = 0; i < M; ++i) {
for (int j = 0; j < N; ++j) {
if (grid[i][j] == 1) {
vector<int> path;
dfs(grid, path, i, j, i, j);
if (!path.empty()) {
s.insert(path);
}
}
}
}
return s.size();
}
void dfs(vector<vector<int>>& grid, vector<int>& path, int sr, int sc, int r, int c) {
if (r < 0 || r >= M || c < 0 || c >= N || grid[r][c] == 0) return;
path.push_back((r - sr) * N + c - sc);
grid[r][c] = 0;
dfs(grid, path, sr, sc, r - 1, c);
dfs(grid, path, sr, sc, r + 1, c);
dfs(grid, path, sr, sc, r, c - 1);
dfs(grid, path, sr, sc, r, c + 1);
}
private:
int M = 0;
int N = 0;
};
参考资料:https://leetcode-cn.com/problems/number-of-distinct-islands/solution/dfszhao-dao-yu-settong-ji-bu-tong-xing-zhuang-dao-/
日期
2019 年 9 月 21 日 —— 莫生气,我若气病谁如意
【LeetCode】694. Number of Distinct Islands 解题报告 (C++)的更多相关文章
- [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 ...
- [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 ...
- [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 ...
- [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 ...
- 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 用了第一种方式, ...
- [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 ...
- 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 ...
- [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 ...
- [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 ...
随机推荐
- Admixture的监督分群(Supervised analysis)
目录 说明 实战 说明 Admixture通过EM算法一般用于指定亚群分类:或者在不知材料群体结构背景下,通过迭代交叉验证获得error值,取最小error对应的K值为推荐亚群数目.如果我们预先已知群 ...
- Linux openssl 升级、降级
Linux openssl 升级.降级 最近遇到一些朋友使用微信退款,报openssl版本为问题,需要对openssl进行降级. 现在环境的openssl版本如下: root@c215a2b695ef ...
- c6和c7
Centos6.x普遍采用 ext3\ext4(Fourth EXtended filesystem)文件系统格式, EXT3 支持的最大 16TB 文件系统和最大 2TB 文件 Ext4 分别支持1 ...
- ping 的原理
ping 的原理ping 程序是用来探测主机到主机之间是否可通信,如果不能ping到某台主机,表明不能和这台主机建立连接.ping 使用的是ICMP协议,它发送icmp回送请求消息给目的主机.ICMP ...
- C/C++ Qt 数据库QSql增删改查组件应用
Qt SQL模块是Qt中用来操作数据库的类,该类封装了各种SQL数据库接口,可以很方便的链接并使用,数据的获取也使用了典型的Model/View结构,通过MV结构映射我们可以实现数据与通用组件的灵活绑 ...
- GraphScope 集群部署
GraphScope 集群部署 1 k8s集群搭建 大致步骤如下: 安装docker.在ubuntu上,可以简单的通过命令sudo apt install docker.io来安装. 安装kubele ...
- day17 阶段测验
题目 1.找出/proc/meminfo文件中以s开头的行,至少用三种方式忽略大小写 有以下几种方法: [root@localhost ~]# grep -iE "^s" /pro ...
- c++ cmake及包管理工具conan简单入门
cmake是一个跨平台的c/c++工程管理工具,可以通过cmake轻松管理我们的项目 conan是一个包管理工具,能够自动帮助我们下载及管理依赖,可以配合cmake使用 这是一个入门教程,想深入了解的 ...
- Learning Spark中文版--第六章--Spark高级编程(1)
Introduction(介绍) 本章介绍了之前章节没有涵盖的高级Spark编程特性.我们介绍两种类型的共享变量:用来聚合信息的累加器和能有效分配较大值的广播变量.基于对RDD现有的transform ...
- Codeforces Round #754 (Div. 2) C. Dominant Character
题目:Problem - C - Codeforces 如代码,一共有七种情况,注意不要漏掉 "accabba" , "abbacca" 两种情况: 使用 ...