要求

  • 给定一个二维数组,只有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. 第13 章 : Kubernetes 网络概念及策略控制

    Kubernetes 网络概念及策略控制 本文将主要分享以下 5 方面的内容: Kubernetes 基本网络模型: Netns 探秘: 主流网络方案简介: Network Policy 的用处: 思 ...

  2. 面试准备——计算机网络(TCP的三次握手和四次挥手)

    一.TCP的报文结构 红色圈标出的是在讨论三次握手和四次挥手时会用到的首部字段: 顺序号(seq):TCP对从网络层传下来的数据报文进行分组,分成一段一段的TCP报文段,并对这些报文段进行编号.seq ...

  3. Mybatis的多表操作

    1.Mybatis多表查询 1.1 一对一查询 1.1.1 一对一查询的模型MapperScannerConfigurer 用户表和订单表的关系为,一个用户有多个订单,一个订单只从属于一个用户 一对一 ...

  4. Java(265-278)【Map】

    1.Map集合概述 是一个接口 键是唯一的 java.util.Map<k,v>集合 Map集合的特点:      1.Map集合是一个双列集合,一个元素包含两个值(一个key,一个val ...

  5. 【CTF】XCTF Misc 心仪的公司 & 就在其中 writeup

    前言 这两题都是Misc中数据包的题目,一直觉得对数据包比较陌生,不知道怎么处理. 这里放两道题的wp,第一题strings命令秒杀觉得非常优秀,另外一题有涉及RSA加密与解密(本文不具体讨论RSA非 ...

  6. redhat7.6 更换 centos7 YUM

    使用yum 遇到如下错误. This system is not registered to Red Hat Subscription Management. You can use subscrip ...

  7. Json文件转换为Excel文件!涉及读文件,时间戳转化,写文档

    一. 思路 今天接到个小任务,让把json文件转换成excel文件,按照列展开. 思路:既然json已经都已经是现成的,那直接将json文件做读操作,在通过不同的key,找到对应的信息,在存到单元格中 ...

  8. F - Lakes in Berland(BFS)

    The map of Berland is a rectangle of the size n × m, which consists of cells of size 1 × 1. Each cel ...

  9. hdu2833 Floyd + dp

    题意:      给你一个无向图,给你两组起点和终点,问你这两组起点和终点的最短路上最多有多少个交点... 思路:      开一个数组dp[i][j]记录最短路上i,j之间的点有多少个,这个数组是根 ...

  10. 病毒木马查杀实战第011篇:QQ盗号木马之专杀工具的编写

    前言 由于我已经在<病毒木马查杀第004篇:熊猫烧香之专杀工具的编写>中编写了一个比较通用的专杀工具的框架,而这个框架对于本病毒来说,经过简单修改也是基本适用的,所以本文就不讨论那些重叠的 ...