统计联通区域块的个数,简单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. Remoting创建远程对象的一个实例:

    private static Lazy<IChannelManager>  channelManager=new Lazy<IChannelManager>(() =>  ...

  2. delete-by-query插件

  3. JQuery Checkbox的change事件

    JQuery   Checkbox的change事件  参考 http://blog.csdn.net/hbhgjiangkun/article/details/8126981   $(functio ...

  4. ArcEngine10.1二次开发错误: 无法嵌入互操作类型,请改用适用的接口

    在之前配置ArcEngine.VS2010二次开发程序的时候,遇见"无法嵌入互操作类型,请改用适用的接口"的错误,在网上查了下,下面引用解决方法. 解决方式为在提示错误的引用上面右 ...

  5. TF-IDF 相关概念

    概念 TF-IDF是一种统计方法,用以评估一字词对于一个文件集或一个语料库中的其中一份文件的重要程度. TF-IDF加权的各种形式常被搜索引擎应用,作为文件与用户查询之间相关程度的度量或评级. 词频( ...

  6. Ext开场布局设计Viewport

    //加载dwr dwr.engine.setAsync(false); //***************************************框架定义部分***************** ...

  7. Redis在CentOS6.4中的安装

    首先,介绍一下Redis数据库.Redis是一种面向“键/值”对数据类型的内存数据库,可以满足我们对海量数据的读写需求. 1)redis的键只能是字符串: 2)redis的值支持多种数据类型: a:字 ...

  8. MySQL使用小记

    时间格式化: select date_format('2008-08-08 22:23:01', '%Y%m%d%H%i%s'); 去重复: use iksdb3; select distinct ` ...

  9. Java并发(8):CountDownLatch、CyclicBarrier、Semaphore、Callable、Future

    CountDownLatch.CyclicBarrier.Semaphore.Callable.Future  都位于java.util.concurrent包下,其中CountDownLatch.C ...

  10. [SmartFoxServer入门]服务器安装

    安装SFS2X: SFS2X平台安装操作和步骤都很简单.我们建议先查看对系统的要求,然后根据你选择的操作系统按照指定的安装向导进行安装. 系统要求: SFS2X是一款支持所有主流操作系统,运行在JVM ...