Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.

Example 1:

11110
11010
11000
00000

Answer: 1

Example 2:

11000
11000
00100
00011

Answer: 3

问题:找出矩阵中前后左右相邻为 1 的区域块块数。

属于 DFS 思想。

将所有 1 塞进一个容器中,从容器中取出一个 1 ,并将相邻走完的 1 也从容器中取出,视为一次取数。重复此操作直至容器中没有元素 1 ,则取出次数就是 1 的区域块块数。

     unordered_map<string, pair<int, int>> val_pos;
vector<vector<char>> theGrid; /**
* remove all land connecting with [i, k] adjecent horizontally or vertically. and the pos.
*/
void removeLand(int i, int k){ theGrid[i][k] = ''; string str = to_string(i) + "_" + to_string(k);
val_pos.erase(str); if (i- >= && theGrid[i-][k] == ''){
removeLand(i-, k);
} if (k+ < theGrid[].size() && theGrid[i][k+] == ''){
removeLand(i, k+);
} if (i+ < theGrid.size() && theGrid[i+][k] == ''){
removeLand(i+, k);
} if (k- >= && theGrid[i][k-] == ''){
removeLand(i, k-);
}
} int numIslands(vector<vector<char>>& grid) { if (grid.size() == ){
return ;
} theGrid = grid;
for ( int i = ; i < grid.size(); i++){
for(int k = ; k < grid[].size(); k++){
if (grid[i][k] == ''){
string str = to_string(i) + "_" + to_string(k);
val_pos[str] = {i, k};
}
}
} int res = ; while(val_pos.size() > ){
int i = val_pos.begin()->second.first;
int k = val_pos.begin()->second.second;
removeLand(i, k);
res++;
} return res;
}

[LeetCode] 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 岛屿的数量

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

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

  4. [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. (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 ...

  7. LeetCode 200. Number of Islands 岛屿数量(C++/Java)

    题目: Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is s ...

  8. Leetcode 200 Number of Islands DFS

    统计联通区域块的个数,简单dfs,请可以参考DFS框架:Leetcode 130 Surrounded Regions DFS class Solution { public: int m, n; b ...

  9. [leetcode]200. Number of Islands岛屿数量

    dfs的第一题 被边界和0包围的1才是岛屿,问题就是分理出连续的1 思路是遍历数组数岛屿,dfs四个方向,遇到1后把周围连续的1置零,代表一个岛屿. /* 思路是:遍历二维数组,遇到1就把周围连续的1 ...

随机推荐

  1. .NET简单的语句

    获取当前时间的代码: Response.Write(DateTime.Now); 第一次加载页面的语句: if (!IsPostBack) { Response.Write("这是第一次加载 ...

  2. [AngularJS 2 实践 一]My First Angular App

    最近一直在看关于AngularJS 2的资料,查看了网上和官网很多资料,接下来就根据官网教程步骤一步步搭建我的第一个Angular App AngularJS 2说明请参考:http://cnodej ...

  3. 配置PPTP服务器

    1.验证内核是否加载了MPPE模块: modprobe ppp-compress-18 && echo MPPE is ok 2.安装所需的软件包: yum -y install pp ...

  4. C#中MD5加密

    C#中进行MD5加密需要使用MD5这个类,这个类位于System.Security.Cryptography命名空间. 转到元数据得知MD5是抽象类和两个静态方法 上代码详解: //得到其静态方法创建 ...

  5. boostrap预定义样式风格

    预定义样式分为五种:primary(首选项).success(成功).info(一般信息).warning(警告).danger(危险),demo如下,设置不同的class展示不同的样式 <!D ...

  6. <customErrors>节点说明1

    <customErrors>节点用于定义一些自定义错误信息的信息.此节点有Mode和defaultRedirect两个属性, 其中defaultRedirect属性是一个可选属性,表示应用 ...

  7. [php基础]PHP.INI配置:Session配置详细说明教程

    网上有很多PHP.INI文件配置的中文说明,但是对于PHP初学者来说在进行PHP运行环境搭建配置时还是容易一头雾水,今天换一种角度来分享如何进行php.ini配置,以求达到解决实际问题的效果,开篇以P ...

  8. Linux中 pid_t 类型的定义.

    说明:涉及到的头文件(.h),目录默认都是基于 /usr/include/ 目录. 1.在 "/sys/types.h"中,有下列内容: #include <bits/typ ...

  9. WARNING OGG-01223 TCP/IP error 111 (Connection refused)

    一:问题描述 GGSCI (source_pc) 64> info all Program     Status      Group       Lag at Chkpt  Time Sinc ...

  10. html表格table设置边框

    对于很多初学HTML的人来说,表格<table>是最常用的标签了,但对于表格边框的控制,很多初学者却不甚其解. 一般我们用表格的时候总会给它个border属性,比如:<table b ...