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. [RxJS] Creation operators: fromEventPattern, fromEvent

    Besides converting arrays and promises to Observables, we can also convert other structures to Obser ...

  2. JPDA 利用Eclipse和Tomcat进行远程调试 --转

    1 编辑tomcat\bin\catalina.bat  ,添加 rem =============================================================== ...

  3. 第一章:在IDEA里搭建基于Forge的Minecraft mod开发环境

    <基于1.8 Forge的Minecraft mod制作经验分享> 网上关于Forge开发环境搭建的文章其实有不少,但大都是基于Eclipse的. 作为用Java开发的环境,怎么能没有ID ...

  4. [HAOI2006]聪明的猴子

    /* 找出能连通所有点的一棵树 是的最大的边最小 很显然就是最小生成树. 堆优化prim. */ #include<iostream> #include<cstring> #i ...

  5. sunny day

    初始学习记录 基于http://www.htmleaf.com/html5/html5muban/20141121552.html模板 <!DOCTYPE html> <html l ...

  6. PHP 单链表

    <?php class Hero { public $no; public $name; public $nickname; public $next=null; public function ...

  7. 智能SQL优化工具--SQL Optimizer for SQL Server(帮助提升数据库应用程序性能,最大程度地自动优化你的SQL语句 )

    SQL Optimizer for SQL Server 帮助提升数据库应用程序性能,最大程度地自动优化你的SQL语句 SQL Optimizer for SQL Server 让 SQL Serve ...

  8. 关于.net 对excel操作的方法

    asp.net打印文件用的最多的一般2种,word和excel,今天在这里整洁一下关于打印excel的几种方式及优缺点 第一种:直接打印html代码,然后将输出类型伪装成excel文件(excel是可 ...

  9. QT5控件-QPushButton和QFocusFrame(按钮和焦点框)

    #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QPushButton> ...

  10. C/C++中的&&和||运算符

    代码: #include <iostream> #include <cstdio> using namespace std; int main(){ ,b=,c=-,d=; d ...