题目

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. C++17新特性optional和string_view

    1. optional的作用 类模板 std::optional 管理一个可选的容纳值,即可以存在也可以不存在的值. 一种常见的 optional 使用情况是一个可能失败的函数的返回值.与其他手段,如 ...

  2. 【视频开发】ONVIF、RTSP/RTP、FFMPEG的开发实录

    ONVIF.RTSP/RTP.FFMPEG的开发实录 前言 本文从零基础一步步实现ONVIF协议.RTSP/RTP协议获取IPC实时视频流.FFMPEG解码.开发环境为WIN7 32位 + VS201 ...

  3. spark 开启job history

    1.首先需要创建spark.history.fs.logDirectory hadoop fs -mkdir hdfs://ns1:9000/user/hadoop/logs 2.修改hadoop-d ...

  4. 进程退出:SIGINT、SIGTERM和SIGKILL区别

    一.SIGINT.SIGTERM和SIGKILL区别 SIGINT与SIGTERM区别1)SIGINT关联ctrl+c2)SIGINT只能结束前台进程3)通过ctrl+c对当前进程发送结束信号,信号被 ...

  5. CMDB资产采集的四种方式

    转 https://www.cnblogs.com/guotianbao/p/7703921.html 资产采集的概念 资产采集的四种方式:Agent.SSH.saltstack.puppet 资产采 ...

  6. 是真的随笔qvq

    DATE:2019.11.20 今天考了试——对光荣爆零.从14:00考到18:30,隔壁计算机教室的电脑弄得心态炸裂了,各种卡,肝了一个下午的两道题以电脑死机没有代码结尾,考完才知道这是最好骗分的两 ...

  7. SSM整合学习 三

    三:整合Mybatis 完整的项目如下 一:下载所需的jar包 <!--日志--><dependency> <groupId>log4j</groupId&g ...

  8. 查询abap 程式应用到系统表table

    *&---------------------------------------------------------------------* *& Report ZMM_TEXT ...

  9. AJAX调用数据,滚动到底部

    最近一个小项目里面,需要使用AJAX去拉取数据,并且直接显示最后一条信息,也就是滚动到底部.实现脚本如下: var scrollHeight = $('.txtBox3').prop("sc ...

  10. MOOC 数据库笔记(三):关系模型之基本概念

    关系模型的基本概念 关系模型简述 1.最早由E.F.Codd在1970年提出. 2.是从表(Table)及表的处理方式中抽象出来的,是在对传统表及其操作进行数学化严格定义的基础上,引入集合理论与逻辑学 ...