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. linux 安装python 和pip

    下载文件 python官网:https://www.python.org/downloads/ 百度网盘http://pan.baidu.com/s/1mixGB12     密码   9nzu [r ...

  2. C-Lodop提示“网页还没下载完毕,请稍等一下再操作.”

    该提示在Lodop旧版本中是: 提示"WebSocket没准备好,点确定继续",提示“C-Lodop没准备好”,新版本修改了该提示的描述“网页还没下载完毕,请稍等一下再操作.”,让 ...

  3. 包packages

    packages里面如何跨模块导入路径: print(dir()) 可以看到__file__ print(os.path.abspaht(__file__)) 可以看到当前绝对路径 import sy ...

  4. JS 单线程和事件循环

    Js 是单线程,js代码从上到下依次执行,比如我们写了两个函数,肯定是上面的函数先执行,下面的函数后执行.但是这种单线程有一个非常大的问题,那就是遇到耗时的任务,后面的任务只能等待它执行完,才能进行. ...

  5. JAVA js WEB 疑难点总结

    1.获取combox的Value 和 Text    $('#id').combobox('getValue').$('#id').combobox('getText'): 2.ajax 直接访问ht ...

  6. controller修改response返回值

    1.responseBodyAdvice2. aop3.过滤器.拦截器

  7. P1567 气温统计

    P1567 题目描述 炎热的夏日,KC 非常的不爽.他宁可忍受北极的寒冷,也不愿忍受厦门的夏天.最近,他开始研究天气的变化.他希望用研究的结果预测未来的天气. 经历千辛万苦,他收集了连续 N(1≤N≤ ...

  8. java 对象转JSON字符串 $ref 错误

    顾名思义,这个是对象转Json时,发生的引用错误. 比较简单的方法是: 使用 帮助方法 https://www.cnblogs.com/hanjun0612/p/9779781.html Conver ...

  9. 一:Newtonsoft.Json 支持序列化与反序列化的.net 对象类型;

    导航目录: Newtonsoft.Json 概述 一:Newtonsoft.Json 支持序列化与反序列化的.net 对象类型:    二:C#对象.集合.DataTable与Json内容互转示例: ...

  10. centos 7创建ss服务(方式二)

    一:安装pip yum install python-pip 如果没有python包则执行命令:yum -y install epel-release: 二:安装SS pip install shad ...