200. Number of Islands(DFS)
'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:
Input:
11110
11010
11000
00000 Output: 1
Example 2:
Input:
11000
11000
00100
00011 Output: 3

遇到1 ,就dfs 吃掉他的邻居。
class Solution {
public:
int numIslands(vector<vector<char>>& grid) {
if(grid.size()==) return ;
int cnt =;
for(int i =;i<grid.size();i++)
for(int j = ;j <grid[].size();j++){
cnt+=grid[i][j]-'';
dfs(grid,i,j);
}
return cnt;
}
void dfs(vector<vector<char>>& grid,int x,int y ){
if(x>= grid.size()||y>=grid[].size()||x<||y<||grid[x][y]=='') return ;
grid[x][y] = '';
dfs( grid,x-,y);
dfs( grid,x+,y);
dfs( grid,x,y-);
dfs( grid,x,y+);
}
};
http://zxi.mytechroad.com/blog/searching/leetcode-200-number-of-islands/
200. Number of Islands(DFS)的更多相关文章
- 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 、694 Number of Distinct Islands 、695. Max Area of Island 、130. Surrounded Regions
两种方式处理已经访问过的节点:一种是用visited存储已经访问过的1:另一种是通过改变原始数值的值,比如将1改成-1,这样小于等于0的都会停止. Number of Islands 用了第一种方式, ...
- (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 ...
- 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
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岛屿个数
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...
- 200. Number of Islands (Graph)
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...
随机推荐
- Ubuntu下创建新用户后,不能使用管理员用户下安装的Anaconda
解决办法: 将管理员用户下Anaconda的安装环境添加到新用户下的环境变量里,具体操作: 系统切换到新用户下: vim .bashrc #添加Anaconda的环境变量 source .bashrc ...
- composer lavarel 安装
一:packagist库:https://packagist.org/packages/laravel/laravel 二:composer安装 // 安装到laravel文件夹 composer c ...
- 阿里云php-7.2.12 安装
安装依赖 yum -y install libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml ...
- chrome自动填表会遮挡input中背景图的问题解决方法
在做某项目登录界面时,发现用户密码框在Chrome自动填充时,input中的背景框会被遮住.网上也搜了一下,没有一个有效的解决方法. 来看csdn的登录界面,也有这个问题. 后来在浏览网页时,无意中发 ...
- Android的TextView设置加粗对汉字无效
//not work textView.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD)); //work! static public voi ...
- Java-IO基础类回忆
好记性不如烂笔头,就拿Java IO来说吧,这部分的基础类我大学都已经学过一遍了,但是现在忘记的差不多了,所以准备写一篇博客,讲这些东西都回忆一下,并且整理一下. 首先借用网上的一张图: 纵向分为字节 ...
- python装饰器语法
@就是decorator,早Python的高效开发中会用到,当然和java的annotation有一定的相似,但又不完全相同,看这篇文章:https://blog.csdn.net/zkp_987/a ...
- 3D Slicer Modify Mouse Event 修改3D Slicer中的鼠标响应事件
在3D Slicer中,我们如果想在自己写的插件中来修改默认的鼠标响应事件的话,就需要先将原有的响应事件链接删除,然后建立自定义的响应事件链接,然后将自己要实现的功能写在响应事件函数中. 比如Slic ...
- react使用apollo简单的获取列表
react yarn add apollo-boost apollo-client react-apollo apollo-cache-inmemory apollo-link-http graphq ...
- 解决ssh出现"Write failed: Broken pipe"问题
用 ssh 命令连接服务器之后,如果一段时间不操作,再次进入 Terminal 时会有一段时间没有响应,然后就出现错误提示: Write failed: Broken pipe 只能重新用 ssh 命 ...