统计联通区域块的个数,简单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. phpexcel相关函数

    1.header [php] header("Content-Type:application/vnd.ms-excel"); header("Content-Dispo ...

  2. 《CSS3秘籍》(第三版)-读书笔记(2)

    第6章 文本格式化 1.  使用字体 字体font-family: 通用的字体样式: serif字体最适用于冗长的文字信息.这种字体使字母主笔画的结尾处会有一些细小的“足”. sans-serif字体 ...

  3. bootstrap的日期插件datetimepicker有问题

    bootstrap的日期插件datetimepicker在chrome中会出现掉下来的现象,而且一直没找到原因,下载最新版的插件直接在各个浏览器中都会掉下来, 问题一直解决不了,转而换其他插件 htt ...

  4. Amr and Chemistry CodeForces 558C(BFS)

    http://codeforces.com/problemset/problem/558/C 分析:将每一个数在给定范围内(10^5)可变成的数(*2或者/2)都按照广搜的方式生成访问一遍,标记上访问 ...

  5. Android Studio 初使用

    Android Studio 更改Eclipse快捷键 Android Studio 更改编码 Android Studio 导包

  6. 用PHP将Unicode 转化为UTF-8

    function unescape($str) { $str = rawurldecode($str); preg_match_all("/(?:%u.{4})|&#x.{4};|& ...

  7. SQLSERVER - Mysql 调试 笔记

    //性能SET STATISTICS IO on;  SET STATISTICS TIME on; //Mysql 切分字符串 CREATE  PROCEDURE proc_split(    in ...

  8. Educational Codeforces Round 10

    A:Gabriel and Caterpillar 题意:蜗牛爬树问题:值得一提的是在第n天如果恰好在天黑时爬到END,则恰好整除,不用再+1: day = (End - Begin - day0)/ ...

  9. D3 学习资源

    发现这个网站还是挺不错的:http://www.ourd3js.com/wordpress/

  10. Webpack使用教程四(Loaders)

    Loaders是webpack最有用的特性之一,通过Loaders,webpack可以预处理源码文件中的Json文件或者将包含新特性的Javascript代码转换成浏览器能处理的JavaScript代 ...