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 16.04 ROS环境配置
最近新入职一家公司,是搞智能无人驾驶的,用的操作系统是Ubuntu和ros,之前没接触过ros系统,既然公司用那就必须的学习啊,话不多说先装它一个ros玩玩... 1. Ubuntu 安装 ROS K ...
- springmvc 返回 404 解决
Idea Maven springmvc spring 项目搭建中/url 可以访问controller,并且能返回正确的ModelAndView,但是页面总是显示404 项目结构: web.xml ...
- 使用 ES2015 编写 Gulp 构建
Gulp 自 v3.9.0 版本增加对 Babel 的支持,也就是说可以使用 ES2015 语法来编写 gulp 任务. 检查 gulp 版本 $ gulp -v 确保 gulp-cli 和 gulp ...
- Ext Js 6+ 如何引入dashboard模版
最近很多人问我在ext js 6+的版本中怎么引入官方的dashboard模版,正好我好久没写博客了,这里我写一篇博客来说明一下. 在这里以ext js 6.2.1版本为例(注:需要安装Sencha ...
- O - Muddy Fields
来源poj2226 Rain has pummeled the cows' field, a rectangular grid of R rows and C columns (1 <= R & ...
- poj2251_kuagnbin带你飞专题一
Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 32684 Accepted: 12529 ...
- Java课程课堂测试05
大致要求:将十道计算题输出至TXT文件,再读取文件至控制台,然后在控制台中输入答案并评判对错 我在大致的要求当中已经将我的设计思路体现了出来 首先,实现计算题的设计,第二步要对计算题循环,将答案存到一 ...
- java导入、导出Excel文件
一.介绍 当前B/S模式已成为应用开发的主流,而在企业办公系统中,常常有客户这样子要求:你要把我们的报表直接用Excel打开(电信系统.银行系统).或者是:我们已经习惯用Excel打印.这样在我们实际 ...
- Cardinal and Ordinal Numbers
Cardinal Numbers Table of Cardinal Numbers Cardinal numbers from 1 through 1,000,000 1 one 11 eleven ...
- [ssh] 通过ssh私钥生成公钥的方法
ssh-keygen -y -f .ssh/id_rsa.key id_rsa.key是私钥.