[LeetCode] 0200. Number of Islands 岛屿的个数
题目
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 岛屿的个数的更多相关文章
- [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 ...
- [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 ...
- 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 ...
- lintcode:Number of Islands 岛屿的个数
题目: 岛屿的个数 给一个01矩阵,求不同的岛屿的个数. 0代表海,1代表岛,如果两个1相邻,那么这两个1属于同一个岛.我们只考虑上下左右为相邻. 样例 在矩阵: [ [1, 1, 0, 0, 0], ...
- 200 Number of Islands 岛屿的个数
给定 '1'(陆地)和 '0'(水)的二维网格图,计算岛屿的数量.一个岛被水包围,并且通过水平或垂直连接相邻的陆地而形成.你可以假设网格的四个边均被水包围.示例 1:11110110101100000 ...
- Leetcode200. Number of Islands岛屿的个数
给定一个由 '1'(陆地)和 '0'(水)组成的的二维网格,计算岛屿的数量.一个岛被水包围,并且它是通过水平方向或垂直方向上相邻的陆地连接而成的.你可以假设网格的四个边均被水包围. 示例 1: 输入: ...
- [leetcode]200. Number of Islands岛屿数量
dfs的第一题 被边界和0包围的1才是岛屿,问题就是分理出连续的1 思路是遍历数组数岛屿,dfs四个方向,遇到1后把周围连续的1置零,代表一个岛屿. /* 思路是:遍历二维数组,遇到1就把周围连续的1 ...
- [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 ...
- [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 ...
随机推荐
- oracle登录信息对应
登录数据库填写的是,配置文件中自己定义的值 2. 查询非纯数字值的字段内容 select trim(translate(RTRIM(LTRIM(ITEM_NUMBER)), '#0123456789' ...
- org.apache.hadoop.conf.Configuration无法引用 解决方法
我用的是Hadoop-common 2.6.4jar,可是明明包里面有这个类却引用不了,然后我看了下包里面是一个抽象类......................................... ...
- git 删除本地分支,删除远程分支
本地分支 git branch -d 分支名 远程分支 git push origin --delete 分支名 查看所有分支 git branch -a
- Python基础(七)——文件和异常
1.1 读取整个文件 我们可以创建一个 test.txt 并写入一些内容,使用 Python 读文件操作,读出文本内容. with open(r'E:\test.txt') as file_objec ...
- C语言合并果子-贪心算法
/*有几堆水果.每次你把两堆东西移到一起,形成更大的一堆.每个动作消耗的能量是两堆水果的总重量.如何把所有的水果堆在一起,消耗最少的能量?*/ 以上是题目,该题首先要读懂题目,每次移到一起以后都要将数 ...
- QT5的QChart使用记录
如果需要在QT中使用QChart类,需要在安装的时候勾选QChart选项,在工程的 .pro 文件里面添加 QT += charts 语句,包含 QChart 头文件就行了. 对于图表的显示,可以先拖 ...
- 第1课,python输出,输入,变量,运算
课程内容: 为什么要学习python 如何学python 实践体验 图片来源网络分享 为什么要学python: 简单 (设计如此) 强大(因为开源,有库) 如何学习python: 变量 --> ...
- 《Effective Objective-C》概念篇
1.运行时 OC 语言由 Smalltalk(20世纪70年代出现的一种面向对象的语言) 演化而来,后者是消息型语言的鼻祖. OC 使用动态绑定的消息结构,在运行时检查对象类型. 使用消息结构的语言, ...
- Tcl语言学习--基础知识
一.脚本.命令和单词符号 一个TCL脚本可以包含一个或多个命令.命令之间必须用换行符或分号隔开. 1.关键字/变量 变量是程序的基础变量组成:变量名.变量值变量名要求:任何字符串都可以作为变量名,区分 ...
- CentOS7搭建jenkins
一.概述 Jenkins是一个开源软件项目,是基于Java开发的一种持续集成工具,用于监控持续重复的工作,旨在提供一个开放易用的软件平台,使软件的持续集成变成可能. 安装环境 操作系统:centos ...