要求

  • 给定一个二维数组,只有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. 17. vue-route详细介绍

    前后端路由的来历 前端如何实现页面跳转但是不刷新? 了解hash和history两种方法 vue-router基本使用 安装vue-router 搭建vue-router框架的步骤 vue-route ...

  2. c++一些概念

    面向对象语言三大特征: 封装,多态,继承 封装: 1.将函数定义到结构体内部,就是封装. 2.编译器会自动传递结构体的指针给函数. 类: 带有函数的结构体,称为类. 成员函数: 结构体里面的函数,称为 ...

  3. 【Git基本命令】

    [基本指令] git init :使目标文件夹变成一个仓库 git add <文件名,含后缀> : 告诉git我要添加文件了 git commit -m "<提交说明> ...

  4. Leedcode算法专题训练(二分查找)

    二分查找实现 非常详细的解释,简单但是细节很重要 https://www.cnblogs.com/kyoner/p/11080078.html 正常实现 Input : [1,2,3,4,5] key ...

  5. JDK8接口新关键字default和static

    JDK8及以后,允许我们在接口中定义static方法和default方法. public interface InterfaceDemo { // static修饰符定义静态方法 static voi ...

  6. 腾讯云TCA开发工程师认证考试

    1.关于云硬盘CBS的描述,错误的是哪一项?(B) A.云硬盘提供数据块级别的数据存储,采用三副本的分布式机制,为 CVM 提供数据可靠性保证 B.云硬盘可在同一可用区中自由挂载.卸载;挂载和卸载过程 ...

  7. 用laravel Maatwebsite\Excel 教你导出漂亮的Excel表单

    先来看效果图 还算是漂亮吧 哈哈哈. 第一步当然是安装包咯 环境要求 PHP: ^7.0Laravel: ^5.5 composer require maatwebsite/excel 目前3.1 只 ...

  8. hdu4403暴力搜索

    题意:      给你一个数字串,让你在里面添加一个=和若干个+,使等式成立. 思路:      lmax最大是15,直接暴搜,无压力,关键是判重,要在答案的时候判重,一开始在进队列之前判的,各种wa ...

  9. Python小游戏 -- 猜数字

    Python初学者小游戏:猜数字 游戏逻辑:电脑随机生成一个数字,然后玩家猜数字,电脑提示猜的数字大了还是小了,供玩家缩小数字范围,达到既定次数后,玩家失败.若在次数内猜对,玩家获胜. 涉及知识点:r ...

  10. POJ2044 深搜+剪枝(云彩下雨)

    题意:        有一个城镇,是4*4的大小的,然后你控制一块云彩,2*2的,你每天可以有9种走的方法,上下左右,或者不动,走的时候可以走1或者2步,云彩所在的地方肯定会下雨,然后给你做多365天 ...