统计联通区域块的个数,简单dfs,请可以参考DFS框架:Leetcode 130 Surrounded Regions DFS

class Solution {
public:
int m, n;
bool is_in(int x, int y)
{
return (x < m ) && (x >= ) && (y < n ) && (y >= );
} void dfs(std::vector<std::vector<char>> &board, int x, int y)
{
board[x][y] = '';
int dir[][] = { { , }, { -, }, { , }, { , - } };
for (int i = ; i < ; ++i){
int tx = x + dir[i][];
int ty = y + dir[i][];
if (is_in(tx, ty) && board[tx][ty] == '')
{
dfs(board, tx, ty);
}
}
} int numIslands(std::vector<std::vector<char>>& grid)
{
m = grid.size();
if (m == ) return ;
n = grid[].size();
if (n == ) return ;
int ans = ;
for (int i = ; i < m; ++i){
for (int j = ; j < n; ++j){
if (grid[i][j] == ''){
dfs(grid, i, j);
ans++;
}
}
}
return ans;
}
};

Leetcode 200 Number of Islands DFS的更多相关文章

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

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

  8. 200. Number of Islands(DFS)

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

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

随机推荐

  1. 使用dynamic动态设置属性值与反射设置属性值性能对比

    static void Main(string[] args) { int times = 1000000; string value = "Dynamic VS Reflection&qu ...

  2. workerman 的属性

    <?php /** * Created by PhpStorm. * User: zeopean * Date: 2016-08-26 * Time: 16:35 */ use Workerma ...

  3. 防止刷新/后退引起的重复提交问题的Java Token代码,非Struts

    贴子转自http://hi.baidu.com/bobylou,转之前并没有验证文章里的方法是不是有效,估计原作者把它放到blog之前应该做过测试了吧. Struts本身有一套完善的防止重复提交表单的 ...

  4. tp 中关于大小写的问题

    ThinkPHP3.2.3升级的若干问题和注意事项(持续更新) 现把ThinkPHP3.2.2在升级到3.2.3的过程中需要注意和可能的问题整理如下: (无论如何,在升级之前请确认备份) 1.首先3. ...

  5. [原]Jenkins(一)---我理解的jenkins是这样的

    /** * lihaibo * 文章内容都是根据自己工作情况实践得出. *版权声明:本博客欢迎转发,但请保留原作者信息! http://www.cnblogs.com/horizonli/p/5330 ...

  6. STL学习之路

    本文面向的读者:学习过C++程序设计语言(也就是说学习过Template),但是还没有接触过STL的STL的初学者.这实际上是我学习STL的一篇笔记,老鸟就不用看了. 什么是泛型程序设计 我们可以简单 ...

  7. web form 复合控件

    DropDownList 下拉列表 会被编译为select option ps.name 服务端常用,id 客户端常用 一般用法: 一.将数据放进去 方法一:同WinForm相同,给定数据源,然后绑定 ...

  8. jquery注意

    具有 true 和 false 两个属性的属性,如 checked, selected 或者 disabled 使用prop(),其他的使用 attr():

  9. java多条件不定条件查询

    网站或各类管理系统都会用到搜索,会用到一个或多个不确定条件搜索,单条件搜索比较简单,有时候会有多个条件共同查询,如果系统中已经提供了相关的方法供你使用最好,像我做这老系统改版,需要添加搜索,就要自己写 ...

  10. React Native填坑之旅--动画

    动画是提高用户体验不可缺少的一个元素.恰如其分的动画可以让用户更明确的感知当前的操作是什么. 无疑在使用React Native开发应用的时候也需要动画.这就需要知道RN都给我们提供了那些动画,和每个 ...