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:

Input:
11110
11010
11000
00000 Output: 1

Example 2:

Input:
11000
11000
00100
00011 Output: 3
-----------------------------------------------------------------------------------------------------------------------------------------
顾名思义,就是求岛屿的个数,此题可以用bfs,也可以用dfs求解,相对来说,用dfs写代码会更简单,但是这个题我用bfs。
这个题用bfs时,不必新设立vis来判断是否访问过,可以把访问到的‘1’的字符中,把这个字符改为'0'就可以。
 
C++代码:
class Solution {
public:
int numIslands(vector<vector<char>>& grid) {
typedef pair<int,int> pii;
queue<pii> q;
int x = ,y = ,xx = ,yy = ;
int dx[] = {,-,,};
int dy[] = {,,,-};
int row = grid.size();
int col;
int sum = ;
if(row > ){
col = grid[].size();
}
else{
col = ;
}
if(row == || col == ){
return ;
}
for(int i = ; i < row; i++){
for(int j = ; j < col; j++){
if(grid[i][j] == ''){
grid[i][j] == '';
q.push(pii(i,j));
while(!q.empty()){
x = q.front().first;
y = q.front().second;
q.pop();
for(int i = ; i < ; i++){
xx = x + dx[i];
yy = y + dy[i];
if(xx >= && xx < row && yy >= && yy < col && grid[xx][yy] == ''){
grid[xx][yy] = '';
q.push(pii(xx,yy));
}
}
}
sum++;
}
}
}
return sum;
}
};
 emmmm,虽然用DFS写代码会更简单些,但是。。。我怎么觉得DFS比BFS更难理解
class Solution {
public:
int numIslands(vector<vector<char>>& grid) {
if(grid.size() == || grid[].size() == ) return ;
int m = grid.size();
int n = grid[].size();
int res = ;
vector<vector<bool> > vis(m,vector<bool>(n,false));
for(int i = ; i < m; i++){
for(int j = ; j < n; j++){
if(!vis[i][j] && grid[i][j] == ''){
DFS(grid,vis,i,j);
res++;
}
}
}
return res;
}
void DFS(vector<vector<char> >& grid,vector<vector<bool> >& vis, int x, int y){
if(x < || x >= grid.size()) return;
if(y < || y >= grid[].size()) return;
if(grid[x][y] != '' || vis[x][y]) return;
vis[x][y] = true;
DFS(grid,vis,x+,y);
DFS(grid,vis,x-,y);
DFS(grid,vis,x,y+);
DFS(grid,vis,x,y-);
}
};

(BFS/DFS) 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. 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. 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 ...

  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. [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. Lodop输出页面input文本框的最新值

    默认使用Lodop打印页面上的文本框等,会发现虽然页面上文本框输入了值,打印预览却是空的,这是由于没有把最新的值传入Lodop. 如图,演示的是Lodop如何输出文本框内的新值,这里整个页面只有inp ...

  2. Socket初见

    前端代码: <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <t ...

  3. JdbcTemplate的运用 (也是数据源的一种)

  4. mysql语句-DDL语句

    SQL分类 1.DDL语句:数据定义语句,用来定义不同的数据段.数据库,表,列,索引等数据表对象,常用语句:create.drop.alter等. 2.DML语句:数据操作语句,用于添加.删除.更新和 ...

  5. springcloud-app

    https://gitee.com/vmaps/springcloud-app https://yq.aliyun.com/articles/329019?spm=a2c4e.11153940.blo ...

  6. PXE网络装机

    PXE网络装机配置 安装CentOS 6.5系统 1.配置服务端IP地址和yum源 略 2.安装配置VSFTP服务 vsftpd 的作用:为客户端提供FTP服务,便于客户端下载操作系统 (1)安装vs ...

  7. linux-shell系列3-wafAPI

    #!/bin/bash datestr=`env LANG=en_US.UTF-8 date -u "+%a, %d %b %Y %H:%M:%S GMT"`pwdstr=`ech ...

  8. 用贝叶斯定理解决三门问题并用Python进行模拟(Bayes' Rule Monty Hall Problem Simulation Python)

    三门问题(Monty Hall problem)也称为蒙提霍尔问题或蒙提霍尔悖论,出自美国的电视游戏节目<Let’s Make a Deal>.问题名字来自该节目的主持人蒙提·霍尔(Mon ...

  9. 【BZOJ2137】submultiple(数论)

    [BZOJ2137]submultiple(数论) 题面 BZOJ 题解 首先不难发现答案就是:\(\displaystyle\prod_{i=1}^n (\sum_{j=1}^{p_i+1}j^k) ...

  10. 【BZOJ2823】[AHOI2012]信号塔(最小圆覆盖)

    [BZOJ2823][AHOI2012]信号塔(最小圆覆盖) 题面 BZOJ 洛谷 相同的题: BZOJ1 BZOJ2 洛谷 题解 模板题... #include<iostream> #i ...