Leetcode200. Number of Islands岛屿的个数
给定一个由 '1'(陆地)和 '0'(水)组成的的二维网格,计算岛屿的数量。一个岛被水包围,并且它是通过水平方向或垂直方向上相邻的陆地连接而成的。你可以假设网格的四个边均被水包围。
示例 1:
输入: 11110 11010 11000 00000 输出: 1
示例 2:
输入: 11000 11000 00100 00011 输出: 3
广度优先搜索
class Solution {
public:
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
int numIslands(vector<vector<char> >& grid)
{
int r = grid.size();
if(r == 0)
{
return 0;
}
int c = grid[0].size();
vector<vector<bool> > visit(r, vector<bool>(c, false));
queue<pair<int, int> > q;
int res = 0;
for(int i = 0; i < r; i++)
{
for(int j = 0; j < c; j++)
{
if(grid[i][j] == '1' && visit[i][j] == false)
{
res++;
visit[i][j] = true;
q.push(make_pair(i ,j));
while(!q.empty())
{
int x = q.front().first;
int y = q.front().second;
q.pop();
for(int i = 0; i < 4; i++)
{
int xx = x + dx[i];
int yy = y + dy[i];
if(xx < 0 || xx >= r || yy < 0 || yy >= c)
continue;
if(visit[xx][yy] == true || grid[xx][yy] == '0')
continue;
visit[xx][yy] = true;
q.push(make_pair(xx, yy));
}
}
}
}
}
return res;
}
};
Leetcode200. Number of Islands岛屿的个数的更多相关文章
- [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 su ...
- lintcode:Number of Islands 岛屿的个数
题目: 岛屿的个数 给一个01矩阵,求不同的岛屿的个数. 0代表海,1代表岛,如果两个1相邻,那么这两个1属于同一个岛.我们只考虑上下左右为相邻. 样例 在矩阵: [ [1, 1, 0, 0, 0], ...
- 200 Number of Islands 岛屿的个数
给定 '1'(陆地)和 '0'(水)的二维网格图,计算岛屿的数量.一个岛被水包围,并且通过水平或垂直连接相邻的陆地而形成.你可以假设网格的四个边均被水包围.示例 1:11110110101100000 ...
- [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] 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 岛屿的数量
Given a boolean 2D matrix, find the number of islands. Notice 0 is represented as the sea, 1 is repr ...
- leetcode200 Number of Islands
""" Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. ...
随机推荐
- Pthread spinlock自旋锁
锁机制(lock) 是多线程编程中最常用的同步机制,用来对多线程间共享的临界区(Critical Section) 进行保护. Pthreads提供了多种锁机制,常见的有:1) Mutex(互斥量): ...
- java.net.UnknownHostException 异常处理(转)
在linux系统下部署Java产品的集群环境时,后台报出如下异常,系统报找不到名为“QATest242”的主机: ERROR - Get local host name failed -com.tr ...
- LUOGU P1505 [国家集训队]旅游 (树链剖分+线段树)
传送门 解题思路 快被调死的码农题,,,其实就是一个边权下放到点权的线段树+树剖. #include<iostream> #include<cstdio> #include&l ...
- Mac OS下使用ll等命令
1. 进入用户bash_profile文件 vi ~/.bash_profile 2. 添加如下命令 alias ll='ls -l' alias la='ls -A' alias l='ls -CF ...
- Netty ByteBuf泄露定位修改。
1. ByteBuf 2. 问题描述 日志记录中报堆外内存溢出. 3. 问题定位及修改 Netty提供了ByteBuf泄露的检测机制. JVM启动参数中添加: -Dio.netty.leakDetec ...
- MyBatis - 输入和输出参数
基础知识 mybatis规定mapp.xml中每一个SQL语句形式上只能有一个@parameterType和一个@resultType 1. 返回值是一个对象的集合,@resultType中只能写该对 ...
- 在当前目录打开DOS命令行窗口
[step1]选中文件夹 [step2]shift + 鼠标右键
- php从5.6升级到php7后,扩展出现segment fault的问题解决
php7的文档中有这样的描述: Both mistakes might cause memory corruptions and segfaults:1)char *str;long str_len; ...
- GitHub for Visual Studio使用讲解
从VS2015起(应该是吧?),微软已经在VS中集成了GitHub,方便开发者对项目进行版本控制. 扩展包下载地址:https://aka.ms/ghfvs 其实VS2015的安装包中已经自带了这个扩 ...
- window环境下Python+OpenCV配置
最近开始学习OpenCV来进行计算机视觉实验,选择了Python作为实验语言,工欲善其事,必先利其器.先总结下安装配置. 现在opencv目测只支持Python2.7X版本的,还依赖于numpy和ma ...