[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 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 解题思路的更多相关文章
- 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 岛屿的数量
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 ...
- [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 ...
- (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 岛屿数量(C++/Java)
题目: 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 DFS
统计联通区域块的个数,简单dfs,请可以参考DFS框架:Leetcode 130 Surrounded Regions DFS class Solution { public: int m, n; b ...
- [leetcode]200. Number of Islands岛屿数量
dfs的第一题 被边界和0包围的1才是岛屿,问题就是分理出连续的1 思路是遍历数组数岛屿,dfs四个方向,遇到1后把周围连续的1置零,代表一个岛屿. /* 思路是:遍历二维数组,遇到1就把周围连续的1 ...
随机推荐
- 将activity设置成dialog样式如何设置窗口大小
具体做法和dialog窗口的设置类似,只需要在onCreate方法中,加入下列代码,既可以控制大小! // 设置窗体大小 ScreenUtil.getScreenWidth(LoadActivity ...
- C#切割指定区域图片操作
使用winform制作了一个切割图片的功能,切一些固定大小的图片,比如头像.界面如图: 打开本地图片 OpenFileDialog opdialog = new OpenFileDialog(); o ...
- C#获取窗口,模拟按键操作
C#获取窗口,模拟按键操作,实现计算器模拟操作.首先引用. using System.Runtime.InteropServices; 使用DllImport引入两个函数: // Get a hand ...
- MVP模式 详解 案例
介绍 MVC: View:对应于布局文件 Model:业务逻辑和实体模型 Controllor:对应于Activity 实际上关于该布局文件中的数据绑定的操作,事件处理的代码都在Activity中,造 ...
- linux查询当前进程数的命令
$command = "ps aux | grep 'zb_insure/get_order_info_from_queue.php' | grep -v 'grep' | grep - ...
- c# 语句关键字
1. yield 它表明一个方法,操作符或者get选择器里会出现迭代. 用yield return 同时返回每一个元素, 返回类型必须是IEnumerable, IEnumerable<T&g ...
- 如何安装Git到MAC OS X
这里介绍两种方式:一,使用Git command-line二,使用GUI工具SourceTree,功能很强大,很方便 在进行安装前,要说一下,Git和SVN一样,都需要创建一个服务器的,他们都可以创建 ...
- web基础--html
WebBasic 1.web应用体系 课程大纲 1.web基础:做网页 2.结构: a.html 勾勒网页结构及内容 b.css ...
- hdu1025 最长不下降子序列nlogn算法
C - DP Crawling in process... Crawling failed Time Limit:1000MS Memory Limit:32768KB 64bit I ...
- C++ template随笔
话题从重用开始说起: 最基本的重用,重用一个方法,被重用的逻辑被抽取封装成为方法,之后我们把方法当成一种工具来使用(处理数据,输入输出,或者改变状态). 来到了面向对象的时代,如果这个方法出现父类上面 ...