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

遇到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)的更多相关文章

  1. Leetcode 200 Number of Islands DFS

    统计联通区域块的个数,简单dfs,请可以参考DFS框架:Leetcode 130 Surrounded Regions DFS class Solution { public: int m, n; b ...

  2. 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 用了第一种方式, ...

  3. (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 ...

  4. 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 ...

  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(其实就是一个深搜)

    题目: 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岛屿个数

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

  9. 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 ...

随机推荐

  1. rabbitmq web管理

    celery突然连接不上rabbitmq server,结果找半天发现是rabbitmq卡的不行... rabbitmq 设置web管理,添加用户 rabbitmqctl list_queues | ...

  2. Spark--sql--所有函数举例(spark-2.x版本)

    ! expr - Logical not. % expr1 % expr2 - Returns the remainder afterexpr1/expr2. Examples: > SELEC ...

  3. 【转】app之YdbOnline说明文档

    概述 YdbOnline是面向网页开发者提供的网页开发工具包. 通过使用YdbOnline,网页开发者可借助YdbOnline高效地使用语音.位置等手机系统的能力,同时可以直接使用清除缓存.扫一扫等A ...

  4. 【译】Apache Kafka支持单集群20万分区

    之前网上关于确定Kafka分区数的博客多多少少都源自于饶军大神的文章,如今他带来了这方面的第二篇文章,特此翻译一下,记录一下其中的要点. 原贴地址: https://www.confluent.io/ ...

  5. vim不支持鼠标中键拷贝粘贴

    ubuntu 和mint默认安装的vim是不支持系统剪切.粘贴版的,需要执行以下安装:sudo apt-get install vim-gnome

  6. 解决JS(Vue)input[type='file'] change事件无法上传相同文件的问题

    Html <input id="file" type="file" accept=".map" onchange="uplo ...

  7. R 540

    好久没写题解了嘻嘻嘻,昨天补edu自闭了一天还没补完fg这div3令人愉悦. A: #include <bits/stdc++.h> #define mk(a,b) make_pair(a ...

  8. python语法_input

    input:与用户的交互,返回用户输入的值 注意:input接受的所有数据都为字符串,即便输入的为数字,依然会被当成字符串

  9. gui小计算器的程序写法

    import java.awt.BorderLayout; import java.awt.EventQueue; import javax.swing.JFrame; import javax.sw ...

  10. 基于 ASP.NET Core 2.1 的 Razor Class Library 实现自定义错误页面的公用类库

    注意:文中使用的是 razor pages ,建议使用 razor views ,使用 razor pages 有一个小坑,razor pages 会用到 {page} 路由参数,如果应用中也用到了这 ...