给定一个由 '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岛屿的个数的更多相关文章

  1. [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 ...

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

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

  3. 200 Number of Islands 岛屿的个数

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

  4. [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 ...

  5. [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 ...

  6. [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 ...

  7. 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 ...

  8. [LintCode] Number of Islands 岛屿的数量

    Given a boolean 2D matrix, find the number of islands. Notice 0 is represented as the sea, 1 is repr ...

  9. leetcode200 Number of Islands

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

随机推荐

  1. 13-7-return的高级使用

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. Python全栈开发:线程、进程和协程

    Python线程 Threading用于提供线程相关的操作,线程是应用程序中工作的最小单元. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #!/usr/bin/env pytho ...

  3. 网站时间显示——基于Date

    网站时间显示 代码实现如下: =============css样式=================== <style> #show{ width: 460px; height: 100p ...

  4. LoadRunner内部结构(1)

    LoadRunner内部结构(1) 根据http://www.wilsonmar.com/1loadrun.htm  翻译: LoadRunner内部结构 1,            被测系统是由驱动 ...

  5. jvisualvm图解【转】

    jvisualvm图解[转]   http://blog.csdn.net/a19881029/article/details/8432368 jvisualvm能干什么:监控内存泄露,跟踪垃圾回收, ...

  6. SpringMVC参数绑定(未完待续)

    1. Strut2与SpringMVC接收请求参数的区别 Struts2通过action类的成员变量接收SpringMVC通过controller方法的形参接收 2. SpringMVC参数绑定流程 ...

  7. leetcode-80-删除排序数组中的重复项②

    题目描述: 第一次提交: class Solution: def removeDuplicates(self, nums: List[int]) -> int: nums.reverse() f ...

  8. Python-线程(1)

    目录 什么是线程 进程与线程的区别 开启线程 为什么要使用线程 线程之间数据是共享的 什么是线程 线程与进程都是虚拟单位,目的是为了更好的描述某种事物 进程与线程的区别 进程:资源单位 线程:执行单位 ...

  9. TSP+期望——lightoj1287记忆化搜索,好题!

    感觉是很经典的题 记忆化时因为不好直接通过E判断某个状态是否已经求过,所以再加一个vis打标记即可 /*E[S][u]表示从u出发当前状态是S的期望*/ #include<bits/stdc++ ...

  10. 83 落单的数 II

    原题网址:http://www.lintcode.com/zh-cn/problem/single-number-ii/ 给出3*n + 1 个的数字,除其中一个数字之外其他每个数字均出现三次,找到这 ...