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 ...
随机推荐
- JDK中Unsafe类详解
Java中Unsafe类详解 在openjdk8下看Unsafe源码 浅析Java中的原子操作 Java并发编程之LockSupport http://hg.openjdk.java.net/jdk7 ...
- YII2 使用phpexcel(干货)
参考:http://www.cnblogs.com/xiaocongjiejie/p/5106249.html http://www.cnblogs.com/xiaocongjiejie/p/5106 ...
- 某公司面试java试题之【一】,看看吧,说不定就是你将要做的题
- 【2019年04月10日】股票的滚动市盈率PE最低排名
仅根据最新的市盈率计算公式进行排名,无法对未来的业绩做出预测. 新钢股份(SH600782) - 滚动市盈率PE:3.87 - 滚动市净率PB:1.29 - 滚动年化股息收益率:1.31% - 钢铁 ...
- iOS开发之--UIImageView的animationImages动画
图片动画实现,代码如下: -(UIImageView *)animationImageView { if (!_animationImageView) { _animationImageView= [ ...
- macOS Sierra(10.12.6), odoo(11.0), Python(3.5.4)配置
欣闻odoo11支持python3环境了,赶紧在mac平台尝试一下: 前期设置,参考另篇文章:macOS Sierra 10.12.6 odoo 10.0 开发环境配置 因为odoo11尚未正式发布, ...
- JS 一张图理解prototype、proto和constructor的关系
转载于原文地址:https://www.cnblogs.com/xiaohuochai/p/5721552.html(感谢大神的总结) 前面的话 javascript里的关系又多又乱.作用域链是一种单 ...
- win10 快速访问关闭
1. 右键 --- 选项 2. 选择:此电脑
- Windows下使用VS2017搭建FLTK开发环境
环境介绍 系统:win10 64位 IDE:VS 2017 Community FLTK版本:1.3.4-2 下载FLTK 截止到本文编写,FLTK的最新稳定版本是1.3.4-2.我们从官网(www. ...
- artTemplate的使用案列
(2) https://github.com/aui/art-template/wiki/syntax:simple