给定一个由 '1'(陆地)和 '0'(水)组成的的二维网格,计算岛屿的数量。一个岛被水包围,并且它是通过水平方向或垂直方向上相邻的陆地连接而成的。你可以假设网格的四个边均被水包围。

示例 1:

输入:
11110
11010
11000
00000 输出: 1

示例 2:

输入:
11000
11000
00100
00011 输出: 3




//章节 - 队列和栈
//二、队列与广度优先搜索
//1.岛屿的个数
/*
算法思想:
这是一道很有意思的题,本质是求矩阵中连续区域的个数,很容易想到需要用深度优先搜索DFS来解,需要建立一个visited数组用来记录某个位置是否被访问过,对于一个为‘1’且未被访问过的位置,递归进入其上下左右位置上为‘1’的数,将其visited对应值赋为true,继续进入其所有相连的邻位置,这样可以将这个连通区域所有的数找出来,并将其对应的visited中的值赋true,找完一次区域后,我们将结果res自增1,然后我们在继续找下一个为‘1’且未被访问过的位置,以此类推直至遍历完整个原数组即可得到最终结果。
*/
//算法实现:
class Solution {
public:
int numIslands(vector<vector<char> > &grid) {
if (grid.empty() || grid[0].empty()) //
return 0;
int m = grid.size(), n = grid[0].size(), res = 0; //m行n列
vector<vector<bool> > visited(m, vector<bool>(n, false)); //将二维数组初始为false,即都未访问
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (grid[i][j] == '1' && !visited[i][j]) { //为1且未被访问,则递归进入其上下左右位置上为‘1’的数
numIslandsDFS(grid, visited, i, j);
++res; //找完一次区域后,结果res自增1
}
}
}
return res;
}
void numIslandsDFS(vector<vector<char> > &grid, vector<vector<bool> > &visited, int x, int y) {
if (x < 0 || x >= grid.size())
return;
if (y < 0 || y >= grid[0].size())
return;
if (grid[x][y] != '1' || visited[x][y]) //不为1或已被访问
return;
visited[x][y] = true; //已访问
numIslandsDFS(grid, visited, x - 1, y); //递归访问上位置
numIslandsDFS(grid, visited, x + 1, y); //递归访问下位置
numIslandsDFS(grid, visited, x, y - 1); //递归访问左位置
numIslandsDFS(grid, visited, x, y + 1); //递归访问右位置
}
};

LeetCode200 岛屿的个数的更多相关文章

  1. [Swift]LeetCode200.岛屿的个数 | 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. lintcode:Number of Islands 岛屿的个数

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

  3. [LeetCode] Number of Distinct Islands II 不同岛屿的个数之二

    Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) conn ...

  4. [LeetCode] Number of Distinct Islands 不同岛屿的个数

    Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) conn ...

  5. leetcode 岛屿的个数 python

      岛屿的个数     给定一个由 '1'(陆地)和 '0'(水)组成的的二维网格,计算岛屿的数量.一个岛被水包围,并且它是通过水平方向或垂直方向上相邻的陆地连接而成的.你可以假设网格的四个边均被水包 ...

  6. 岛屿的个数12 · Number of Islands12

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

  7. lintcode433 岛屿的个数

    岛屿的个数 给一个01矩阵,求不同的岛屿的个数. 0代表海,1代表岛,如果两个1相邻,那么这两个1属于同一个岛.我们只考虑上下左右为相邻. 您在真实的面试中是否遇到过这个题? Yes 样例 在矩阵: ...

  8. [LeetCode] 711. Number of Distinct Islands II 不同岛屿的个数之二

    Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) conn ...

  9. [LeetCode] 694. Number of Distinct Islands 不同岛屿的个数

    Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) conn ...

随机推荐

  1. When you received Ubuntu...

    翻译软件 Goldendict 安装命令: sudo apt install goldendict 在 dit -> Dictinoaries -> Websites 中添加有道的链接: ...

  2. 题解-CF163E e-Government

    题面 CF163E e-Government 给 \(n\) 个字符串 \(s_i\) 和 \(q\) 个询问,刚开始字符串都服役.每次操作将集合中的一个字符串设为退役或服役,或查询与文本串 \(S_ ...

  3. 微信小程序 rich-text 修改照片

    <view> <rich-text nodes="{{delcon}}" /> </view> data: { delcon:'' }, var ...

  4. JUC(二):CAS及ABA

    CAS是什么? 比较并交换. CAS示例 package com.chinda.java.audition; import java.util.concurrent.atomic.AtomicInte ...

  5. v-once

    v-once 使用了这个指令,那么这个值将只会渲染一次,后续将不会再被更改 初始运行结果: 在控制台中分别修改他们的值: 可以看到使用了v-once指令的插值没有被修改.

  6. Jmeter连接redis

    介绍:现在有很多数据不是存储在数据库而是存储在Redis中 Redis数据库存储数据环境 不用每次都去数据库读取数据 可以有效的优化服务器性能. 下面介绍使用jmeter如何读取redis 一.首先创 ...

  7. 网络编程-python实现-TCP实现文件下载(1.1.4)

    @ 目录 代码实现 代码实现 客户端 from socket import * def main(): # 创建socket tcp_client_socket = socket(AF_INET, S ...

  8. 使用基于Vue.js和Hbuilder的混合模式移动开发打造属于自己的移动app

    近几年,混合模式移动应用的概念甚嚣尘上,受到了一些中小型企业的青睐,究其原因,混合模式开发可以比传统移动开发节约大量的开发成本和人力成本. Hybrid App(混合模式移动应用)是指介于web-ap ...

  9. 根据数据库反向生成PD

    原博文 http://www.360doc.com/content/14/0820/20/12385684_403420399.shtml 步骤 File___Reverse Engineer___D ...

  10. MySQL管理基础

    #1.数据库连接管理 mysql命令说明 第一个功能:连接数据库(在前面mysql命令的使用里面讲解了,这里就不讲解了) 第二个功能:mysql客户端自带的命令功能 mysql命令的使用(mysql ...