给定一个由 '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. CSS三大特性之优先级顺序

    id选择器>类选择器>标签选择器>通配符>继承>浏览器默认

  2. java_缓冲流(字节输入流)

    /** * java.iko.BufferedInputStream extends InputStream * BufferedInputStream:字节缓冲输入流 * 构造方法: * Buffe ...

  3. spring中依赖注入

    理解依赖注入:参考https://blog.csdn.net/taijianyu/article/details/2338311 一.依赖注入让bean与bean之间以配置文件组织在一起,而不是以硬编 ...

  4. 进程监控驱动 PsSetCreateProcessNotifyRoutine

    函数原型: NTSTATUS PsSetCreateProcessNotifyRoutine( _In_ PCREATE_PROCESS_NOTIFY_ROUTINE NotifyRoutine, _ ...

  5. <a>中的背景色变大

    想要调整文字链接背景颜色或图片的大小可以用padding属性: 但火狐和IE数值相同显示相同,但与360数值相同显示不同(上下宽度会变小.)

  6. Java中Arrys数组常用的方法

    Arrys常用方法 1.toString() Arrays.toString(arr)        //打印数组的内容,观察数组元素的值 2.sort() Arrays.sort(arr);     ...

  7. Java 怎样实现调用其他方法

    Java主类的main方法调用其他方法 方法1: funA()方法设为静态方法. 当主类加载到内存,funA()分配了入口地址,主要代码如下: public class test{ static vo ...

  8. Redis相关语法

    设置用户密码 config set requirepass yourPassword

  9. lync sdk 二次开发

    1.关于 UI Suppression Mode http://blog.thoughtstuff.co.uk/2014/08/the-6-things-you-need-to-know-about- ...

  10. vue + element-ui实现简洁的导入导出功能

    1.安装ElementUI模块 cnpm install element-ui -S 2.在main.js中引入 import ElementUI from 'element-ui' import ' ...