[刷题] 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 ...
随机推荐
- Dynamics CRM使用Web Api时如果参数里面包含"&"的时候的处理方法
当我们使用Dynamics CRM的Api的时候如果遇到查询字段的参数里面有&符号的话会影响Api的取值直接报错.原因是因为&符号在Url上面是一个关键字,这个关键字可以截断Url表示 ...
- BUAA_OO_第一单元
BUAA_OO_2020_UNIT1 一.程序结构分析 第一次作业 UML & Mertrics 由于数据处理简单,第一次作业中笔者发挥了面向过程的思想,将项转换成Biginteger, ...
- 死磕Spring之AOP篇 - 初识JDK、CGLIB两种动态代理
该系列文章是本人在学习 Spring 的过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring 源码分析 GitHub 地址 进行阅读. Spring 版本:5.1 ...
- 这样介绍Ribbon,从此任何问题也难不住你
Springcloud的核心组件之Ribbon 上篇文章详细介绍了springcloud的注册中心Eureka,那么这篇文章则会介绍springcloud的另外一个组件Spring Cloud Rib ...
- 尝试做一个.NET模板填充导出Excel工具
园友好,最近晚辈延续上篇后尝试进阶做成Excel模板填充数据生成工具 MiniExcel Template. 主要特点 同样以Stream流.延迟查询避免全部数据载入内存情况,做到1GB内存降低到只需 ...
- c++排序相关的参数“cmp“的用法及理解
对sort函数(需要algorithm头文件),它的cmp可以是"函数",也可以是"对象" bool myfunction (int i,int j) { re ...
- Spring Security框架中踢人下线技术探索
1.背景 在某次项目的开发中,使用到了Spring Security权限框架进行后端权限开发的权限校验,底层集成Spring Session组件,非常方便的集成Redis进行分布式Session的会话 ...
- (二) LDAP 安装
LDAP 安装 参考:https://blog.51cto.com/bigboss/2341986
- lumen Rest API 起步
lumen Rest API 起步 修改项目文件 .env DB_DATABASE=<数据库名> DB_USERNAME=<数据库用户名> DB_PASSWORD=<数据 ...
- POJ2155二维线段树
题意: 给一个n*n的01矩阵,然后有两种操作(m次)C x1 y1 x2 y2是把这个小矩形内所有数字异或一遍,Q x y 是询问当前这个点的值是多少?n<=1000 m<=5 ...