分析:经典连通分量问题

图:

  节点:所有1的位置

  边:两个相邻的1的位置有一条边

BFS/DFS (DFS使用递归,代码较短)

  选一个没标记的点,然后搜索,扩展4个邻居(如果有),直到不能扩展

  每一次是一个连通分量

  难点:标记节点——判重

C++

DFS

 class Solution {
public:
void help(vector<vector<bool>>& a, int x, int y) {
if ((x < ) || (x >= a.size()) || (y < ) || (y >= a[x].size()) || a[x][y] == false) {
return ;
}
a[x][y] = false;
help(a, x + , y);
help(a, x - , y);
help(a, x, y + );
help(a, x, y - );
}
/**
* @param grid a boolean 2D matrix
* @return an integer
*/
int numIslands(vector<vector<bool>>& grid) {
// Write your code here
int ans = ;
for (int i = ; i < grid.size(); ++i) {
for (int j = ; j < grid[i].size(); ++j) {
if (grid[i][j] == true) {
help(grid, i, j);
++ans;
}
}
}
return ans;
}
};

C++

BFS

 class Solution {
public:
void help(vector<vector<bool>>& a, int x, int y) {
queue<pair<int, int> > q;
const int dx[] = {-, , , };
const int dy[] = {, , -, };
a[x][y] = false;
for (q.push(make_pair(x, y)); !q.empty(); q.pop()) {
x = q.front().first;
y = q.front().second;
for (int i = ; i < ; ++i) {
int nx = x + dx[i];
int ny = y + dy[i];
if ((nx >= ) && (nx < a.size()) && (ny >= ) &&(ny < a[nx].size()) && (a[nx][ny] == true)) {
a[nx][ny] = false;
q.push(make_pair(nx, ny));
}
}
}
}
/**
* @param grid a boolean 2D matrix
* @return an integer
*/
int numIslands(vector<vector<bool>>& grid) {
// Write your code here
int ans = ;
for (int i = ; i < grid.size(); ++i) {
for (int j = ; j < grid[i].size(); ++j) {
if (grid[i][j] == true) {
help(grid, i, j);
++ans;
}
}
}
return ans;
}
};

LintCode: Number of Islands的更多相关文章

  1. [LintCode] Number of Islands(岛屿个数)

    描述 给一个01矩阵,求不同的岛屿的个数. 0代表海,1代表岛,如果两个1相邻,那么这两个1属于同一个岛.我们只考虑上下左右为相邻. 样例 在矩阵: [ [1, 1, 0, 0, 0], [0, 1, ...

  2. [LintCode] Number of Islands 岛屿的数量

    Given a boolean 2D matrix, find the number of islands. Notice 0 is represented as the sea, 1 is repr ...

  3. LintCode "Number of Islands II"

    A typical Union-Find one. I'm using a kinda Union-Find solution here. Some boiler-plate code - yeah ...

  4. LintCode 433. 岛屿的个数(Number of Islands)

    LintCode 433. 岛屿的个数(Number of Islands) 代码: class Solution: """ @param grid: a boolean ...

  5. 433. Number of Islands【LintCode java】

    Description Given a boolean 2D matrix, 0 is represented as the sea, 1 is represented as the island. ...

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

  7. [LeetCode] Number of Islands II 岛屿的数量之二

    A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand oper ...

  8. [LeetCode] 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. 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 ...

随机推荐

  1. 前端使用AngularJS的$resource,后端ASP.NET Web API,实现分页、过滤

    在上一篇中实现了增删改查,本篇实现分页和过滤. 本系列包括: 1.前端使用AngularJS的$resource,后端ASP.NET Web API,实现增删改查2.前端使用AngularJS的$re ...

  2. indy10的idhttpServer应答字符串

    indy10的idhttpServer应答字符串 先看应答字符串的代码: procedure TIdIOHandler.Write(const AOut: string; AByteEncoding: ...

  3. 理解 block

    开始:Block 简介 Block 是 iOS 4.0 和 Mac OSX 10.6 引入的一个新特性. Block 可以极大的简化代码. 他们可以帮助你减少代码, 减少对代理的依赖, 并且写出更加简 ...

  4. jQuery Pagination分页插件

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  5. Android 4.4 Kitkat Phone工作流程浅析(八)__Phone状态分析

    本文来自http://blog.csdn.net/yihongyuelan 转载请务必注明出处 本文代码以MTK平台Android 4.4为分析对象.与Google原生AOSP有些许差异.请读者知悉. ...

  6. CentOS安装sctp协议

    转自:http://blog.csdn.net/fly_yr/article/details/48375247 序 最近学习Unix网络编程,在第10章节,SCTP客户/服务器 程序实现时,发现很多由 ...

  7. 给hmailserver添加SSL支持

    我们使用stunnel来给hmailserver添加ssl支持,stunnel是一个开源跨平台提供全局TLS/SSL支持的软件,它可以给很多本身不支持ssl的软件来提供安全的加密连接,同样可以用于hm ...

  8. wince程序调用另外一个wince exe程序?

    记住:要释放句柄 清空内存(当前程序) 在虚拟机下测试如图: 在reyo.ini文件中配置另一wince执行程序的路径,如果不配置程序会报错: 如果配置的程序不存在报错: 没有问题就调用所在位置的wi ...

  9. Swift - CALayer的contents属性动画

    Swift - CALayer的contents属性动画 效果 源码 https://github.com/YouXianMing/Swift-Animations // // LiveImageVi ...

  10. H2:开源内存数据库引擎

    本资源由 伯乐在线 - 刘立华 整理 H2是一个开源的内存数据库.Java编写.快速.小巧(1.5MB jar包)还提供了Web控制台管理数据库内容. 主要功能 非常快速的数据库引擎. 开源. Jav ...