统计联通区域块的个数,简单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. 一个简单的SNTP客户端

    借鉴于python网络编程攻略 #/usr/local/bin/python3.5 #coding:utf-8 import socket, struct, time NTP_server = &qu ...

  2. JDBC第一天

    (一)JDBC访问数据库的工作过程:               <1>加载驱动,建立连接 oracle数据库:在项目中导入ojdbc.jar包 数据库驱动路径:String driver ...

  3. ubuntu14.04 的网络配置

    为eth0 配置网络 vi /etc/network/intefaces 添加以下内容 auto eth0 iface eth0 inet static address 192.168.0.10 ne ...

  4. quick-cocos2d-x之testlua之VisibleRect.lua

    require "extern" --这个类找到了可视区域的9个点的坐标:左上.上的中点.右上.左的中点.左下.下的中点.右下.右的中点.一般用于使用相对坐标的场合,解决自适应屏幕 ...

  5. 管理表空间和数据文件<六>

    数据库管理 -- 管理表空间和数据文件  介绍 表空间是数据库的逻辑组成部分.从物理上讲,数据库数据存放在数据文件中:从逻辑上讲,数据库则是存放在表空间中,表 空间由一个或多个数据文件组成. 数据库 ...

  6. Sql 常见面试题

    SQL面试题(1) create table testtable1(id int IDENTITY,department varchar(12) ) select * from testtable1i ...

  7. StringGrid 实例1:初始化StirngGrid的首行和首列

    实例1:初始化StirngGrid的首行和首列

  8. C/C++字符串函数之复制函数

    突然发现对字符串函数缺乏系统的了解,所以花了一点时间专门整理下,在此记录之,以方便自己及有需要的人使用. C/C++字符串函数的头文件:string.h 复制函数主要有4个,如下: 1.char * ...

  9. [转]MySQL中存储过程权限问题

    MySQL中以用户执行存储过程的权限为EXECUTE 比如我们在名为configdb的数据库下创建了如下存储过程,存储过程的定义者为user_admin use configdb; drop proc ...

  10. SQL基础语法等

    --1.while循环 declare @sum int declare @i int ) begin set @sum =@sum+@i ) print @i end print @sum --2. ...