要求

  • 给定一个二维数组,只有0和1两个字符,1代表陆地,0代表水域,纵向和横向的陆地连接成岛屿,被水域隔开,求出地图中有多少岛屿

思路

  • 对要查找的区域进行双重循环遍历,寻找陆地
  • 从陆地初始点开始进行深度优先遍历
  • 如:从(0,0)开始深度遍历,填充岛屿,(0,1)已经访问过,(0,2)为水域,直到(2,2)再开始深度遍历

实现

  • res:有多少个岛屿
  • 递归条件:在区域内,未被访问过,是陆地
  • 终止条件融入到了if语句中
 1 class Solution {
2
3 private:
4 int d[4][2] = {{0,1},{1,0},{0,-1},{-1,0}};
5 int m,n;
6 vector<vector<bool>> visited;
7
8 bool inArea( int x, int y){
9 return x >= 0 && x < m && y >= 0 && y < n;
10 }
11
12 // 从grid[x][y]开始,进行floodfill
13 // 保证(x,y)合法,且grid[x][y]是没有被访问过的陆地
14 void dfs( vector<vector<char>>& grid, int x, int y ){
15
16 visited[x][y] = true;
17 for( int i = 0 ; i < 4 ; i ++ ){
18 int newx = x + d[i][0];
19 int newy = y + d[i][1];
20 // if语句保证访问合法,不需再写递归终止条件
21 if( inArea(newx, newy) && !visited[newx][newy] && grid[newx][newy] == '1')
22 dfs( grid, newx, newy );
23 }
24 return;
25 }
26
27 public:
28 int numIslands(vector<vector<char>>& grid) {
29 m = grid.size();
30 if( m == 0 )
31 return 0;
32 n = grid[0].size();
33
34 visited = vector<vector<bool>>(m, vector<bool>(n,false));
35
36 int res = 0;
37 for( int i = 0 ; i < m ; i ++ )
38 for( int j = 0 ; j < n ; j ++ )
39 if( grid[i][j] == '1' && !visited[i][j] ){
40 res ++;
41 dfs( grid, i, j );
42 }
43 return res;
44 }
45 };

相关

  • 130 Surrounded Regions
  • 417 Pacific Atlantic Water Flow

[刷题] 200 Number of Islands的更多相关文章

  1. leetcode 200. Number of Islands 、694 Number of Distinct Islands 、695. Max Area of Island 、130. Surrounded Regions

    两种方式处理已经访问过的节点:一种是用visited存储已经访问过的1:另一种是通过改变原始数值的值,比如将1改成-1,这样小于等于0的都会停止. Number of Islands 用了第一种方式, ...

  2. 【刷题-LeetCode】200 Number of Islands

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

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

  4. (BFS/DFS) 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]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 ...

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

  7. 【LeetCode】200. Number of Islands 岛屿数量

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://le ...

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

  9. Java for 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 ...

随机推荐

  1. 你才不是只会理论的女同学-seata实践篇

    本文主要内容为seata的实践篇,理论知识不懂的请参考前文: 我还不懂什么是分布式事务 主要介绍两种最常用的TCC和AT模式. 环境信息: mysql:5.7.32 seata-server:1.4. ...

  2. 在Vue中使用sass和less,并解决报错问题(this.getOptions is not a function)

    使用 Less 下载依赖:npm install less less-loader 在mian.js 中添加: import less from "less"; Vue.use(l ...

  3. 字节、位、bit、byte、KB、B、字符之间的关系

    一.bit 位bit就是位,也叫比特位,是计算机表示数据最小的单位,例如 1b ,2b,3b..... 说白了就是0或者1:计算机内存中的存储都是01这两个东西. 二.byte 字节byte就是字节 ...

  4. 采用QT技术,开发OFD电子文档阅读器

    前言 ofd作为板式文档规范,相当于国产化的pdf.由于pdf标准制定的较早,相关生态也比较完备,市面上的pdf阅读器种类繁多.国内ofd阅读器寥寥无几,作者此前采用wpf开发了一款阅读器,但该阅读器 ...

  5. CentOS8搭建FTP服务器

    2021.2.20 更新 1 概述 文章核心: CentOS8使用vsftpd搭建FTP服务器 安装以及测试的详细过程 2 安装 2.1 安装vsftpd+ftp sudo yum install - ...

  6. HUAWEI AppGallery Connect翻译服务升级,方便应用出海本地化

    HUAWEI AppGallery Connect翻译服务全新升级,携手新宇智慧.语言桥等多家服务商为全球开发者提供优质的翻译服务,解决应用在全球分发时面临的多语言本地化问题,通过平台化与全环节的翻译 ...

  7. Jenkins 自定义构建结果

    1. Jenkins 构建原理 2. 脚本执行失败立即停止执行 3. 脚本执行失败继续后面的执行但最终的结果是构建失败 1. Jenkins 构建原理 Jenkins 的构建成功和脚本执行成功是两个事 ...

  8. mooc人大单元测试4

    @font-face { font-family: Wingdings } @font-face { font-family: 宋体 } @font-face { font-family: " ...

  9. 利用主机域名配置Apache的虚拟主机功能

    利用主机域名配置Apache的虚拟主机功能,可以减轻服务器为每一个网站分配IP地址的压力.而且根据不同的主机域名可以输出不同的网页内容.要实现此项功能,可以通过修改/etc/host文件来强制定义IP ...

  10. SSL证书及HTTPS服务器

    1. 域名 在万网购买,略 2. 云服务器 阿里云购买,略 3. 安装lnmp 使用lnmp.org程序,略 4. 申请证书 阿里云-管理控制台-安全(云盾)-证书服务-购买证书证书类型: 免费型DV ...