[刷题] 200 Number of Islands
要求
- 给定一个二维数组,只有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的更多相关文章
- 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 用了第一种方式, ...
- 【刷题-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. ...
- 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 ...
- (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 ...
- [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 ...
- 【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 ...
- 【LeetCode】200. Number of Islands 岛屿数量
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://le ...
- 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 ...
- 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 ...
随机推荐
- InlineHook
前言 IATHOOK局限性较大,当我们想HOOK一个普通函数,并不是API,或者IAT表里并没有这个API函数(有可能他自己LoadLibrary,自己加载的),那我们根本就从导入表中找不到这个函数, ...
- 201871030105-陈啟程 实验三 结对项目—《D{0-1}KP 实例数据集算法实验平台》项目报告
项目 内容 课程班级博客 https://edu.cnblogs.com/campus/xbsf/2018CST 这个作业要求链接 https://www.cnblogs.com/nwnu-daizh ...
- 基于注解的springboot+mybatis的多数据源组件的实现
通常业务开发中,我们会使用到多个数据源,比如,部分数据存在mysql实例中,部分数据是在oracle数据库中,那这时候,项目基于springboot和mybatis,其实只需要配置两个数据源即可,只需 ...
- Spring Boot 实现配置文件加解密原理
Spring Boot 配置文件加解密原理就这么简单 背景 接上文<失踪人口回归,mybatis-plus 3.3.2 发布>[1] ,提供了一个非常实用的功能 「数据安全保护」 功能,不 ...
- house_of_storm 详解
house_of_storm 漏洞危害 House_of_storm 可以在任意地址写出chunk地址,进而把这个地址的高位当作size,可以进行任意地址分配chunk,也就是可以造成任意地址写的后果 ...
- xman_2019_format(非栈上格式化字符串仅一次利用的爆破)
xman_2019_format(非栈上格式化字符串仅一次利用的爆破) 首先检查一下程序的保护机制 然后用IDA分析一下 存在后门 首先malloc了一片堆空间,读入数据 把刚刚读入的数据当作格式化字 ...
- shell 使用 cat 配合 EOF 创建文件并写入多行内容
之前折腾 GtiHub Actions 想实现提交 issue 后将 issue 的内容生成一个 Markdown 文件提交到仓库,从而实现自动发布到 GitHub Pages 的目的.倒是有一些现成 ...
- Django模板引擎
Django作为Web框架,需要一种很便利的方法动态地生成 HTML 网页,因此有了模板这个概念.模板包含所需 HTML 的部分代码以及一些特殊语法,特殊语法用于描述如何将视图传递的数据动态插入HTM ...
- Springboot+Vue前后端分离的博客项目
项目介绍 演示站(服务器已过期):http://blog.hanzhe.site 开源项目地址 ( 求给个Star ):https://gitee.com/zhang_hanzhe/blog 前端采用 ...
- 基于MATLAB的手写公式识别(9)
基于MATLAB的手写公式识别(9) 1.2图像的二值化 close all; clear all; Img=imread('drink.jpg'); %灰度化 Img_Gray=rgb2gray(I ...