题目

Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.

给定一个由 '1'(陆地)和 '0'(水)组成的的二维网格,计算岛屿的数量。一个岛被水包围,并且它是通过水平方向或垂直方向上相邻的陆地连接而成的。你可以假设网格的四个边均被水包围。

Example 1:

Input:
11110
11010
11000
00000 Output: 1

Example 2:

Input:
11000
11000
00100
00011 Output: 3

解法一

思路:逐元素遍历输入的二维数组,遇到1时展开BFS搜索,并用记录下遇到过的节点。

参考资料:https://www.cnblogs.com/grandyang/p/4402656.html

#include <vector>
#include <queue> using namespace std; class Solution {
public:
int numIslands(vector<vector<char>>& grid) {
if (grid.empty() || grid[0].empty()) {
return 0;
}
int n_row = grid.size();
int n_col = grid[0].size();
int res = 0;
vector<vector<bool>> visited(n_row, vector<bool>(n_col));
vector<int> dirX{-1, 0, 1, 0}, dirY{0, 1, 0, -1}; for (int i = 0; i < n_row; i++) {
for (int j = 0; j < n_col; j++) {
if (grid[i][j] == '0' || visited[i][j])
continue;
++res;
queue<int> q{{i * n_col + j}};
while (!q.empty()) {
int t = q.front(); q.pop();
for (int k = 0; k < 4; k++) {
int x = t / n_col + dirX[k];
int y = t % n_col + dirY[k];
if (x < 0 || x >= n_row || y < 0 || y >= n_col || grid[x][y] == '0' || visited[x][y])
continue;
visited[x][y] = true;
q.push(x * n_col + y);
}
}
}
}
return res;
}
};

[LeetCode] 0200. Number of Islands 岛屿的个数的更多相关文章

  1. [LeetCode] 200. Number of Islands 岛屿的数量

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

  2. [leetcode]200. Number of Islands岛屿个数

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

  3. LeetCode 200. Number of Islands 岛屿数量(C++/Java)

    题目: Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is s ...

  4. lintcode:Number of Islands 岛屿的个数

    题目: 岛屿的个数 给一个01矩阵,求不同的岛屿的个数. 0代表海,1代表岛,如果两个1相邻,那么这两个1属于同一个岛.我们只考虑上下左右为相邻. 样例 在矩阵: [ [1, 1, 0, 0, 0], ...

  5. 200 Number of Islands 岛屿的个数

    给定 '1'(陆地)和 '0'(水)的二维网格图,计算岛屿的数量.一个岛被水包围,并且通过水平或垂直连接相邻的陆地而形成.你可以假设网格的四个边均被水包围.示例 1:11110110101100000 ...

  6. Leetcode200. Number of Islands岛屿的个数

    给定一个由 '1'(陆地)和 '0'(水)组成的的二维网格,计算岛屿的数量.一个岛被水包围,并且它是通过水平方向或垂直方向上相邻的陆地连接而成的.你可以假设网格的四个边均被水包围. 示例 1: 输入: ...

  7. [leetcode]200. Number of Islands岛屿数量

    dfs的第一题 被边界和0包围的1才是岛屿,问题就是分理出连续的1 思路是遍历数组数岛屿,dfs四个方向,遇到1后把周围连续的1置零,代表一个岛屿. /* 思路是:遍历二维数组,遇到1就把周围连续的1 ...

  8. [LeetCode] 305. Number of Islands II 岛屿的数量 II

    A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand oper ...

  9. [LeetCode] Number of Islands 岛屿的数量

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

随机推荐

  1. 历时一年《Python自动化测试实战》终于出版!!!

    一.为什么会写这本书 1.系统梳理.可以加深自己对测试知识体系的系统梳理 2.名气.增加个人的名气,比如:面试时,可以很自豪的说,我是xxxx书的作者 3.利他.帮助有需要的学习者更系统.完备的学习和 ...

  2. C# COM组的开发以及调用

    一.用C#编写一个COM组件 1.       打开Visual Studio2008,[文件]->[新建]->[项目] 2.       项目类型=Visual C#,模版=类库,名称= ...

  3. Java分布式:分布式锁之数据库实现

    Java分布式:分布式锁之数据库实现 分布式锁系列教程重点分享锁实现原理 锁实现原理 创建一张名为methodLock的数据库表,为方法名字段(method_name)添加唯一性约束. CREATE ...

  4. [ARM-Linux开发] 主设备号--驱动模块与设备节点联系的纽带

    一.如何对设备操作 linux中对设备进行操作是通过文件的方式进行的,包括open.read.write.对于设备文件,一般称其为设备节点,节点有一个属性是设备号(主设备号.次设备号),其中主设备号将 ...

  5. adb devices命令链接设备失败 解决办法

    使用adb devices命令链接设备失败 1,检查adb的环境配置是否存在问题 参见我的博客(sdk配置):https://www.cnblogs.com/changpuyi/p/9459025.h ...

  6. opencv之重映射

    好久没写呆码了 今天发个重映射 #include "opencv2/video/tracking.hpp" #include "opencv2/imgproc/imgpr ...

  7. centos7中mysql的rpm包安装

    解决依赖 yum remove mysql-libs 执行命令:yum -y install autoconf 安装依赖 yum -y install autoconf 安装mysql rpm -iv ...

  8. UnicodeEncodeError: 'ascii' codec can't encode characters in position 18-22: ordinal not in range(128)

    文件,放在site-package \Python27\Lib\site-packages     sitecustomize.py # -*- coding: utf-8 -*- import sy ...

  9. @SuppressWarnings注解用法

    @SuppressWarnings注解主要用在取消一些编译器产生的警告对代码左侧行列的遮挡,有时候这会挡住我们断点调试时打的断点. 如图所示: 这时候我们在方法上加上@SuppressWarnings ...

  10. java之mybatis之一对多关联映射

    1.在一对多的关联映射中,表结构如下 2.实体类结构 User.java public class User implements Serializable{ private int id; priv ...