分析:经典连通分量问题

图:

  节点:所有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. Delphi 判断时间是否合法 -IsValidDateTime、IsValidDate、IsValidTime、IsValidDateDay

    DateUtils.IsValidDateTimeDateUtils.IsValidDateDateUtils.IsValidTimeDateUtils.IsValidDateDayDateUtils ...

  2. Java多线程中join方法的理解

    thread.Join把指定的线程加入到当前线程,可以将两个交替执行的线程合并为顺序执行的线程.比如在线程B中调用了线程A的Join()方法,直到线程A执行完毕后,才会继续执行线程B. t.join( ...

  3. ExtJS 4.2 教程-03:使用Ext.define自定义类

    转载自起飞网,原文地址:http://www.qeefee.com/extjs-course-3-define-classes ExtJS 4.2 教程-01:Hello ExtJS ExtJS 4. ...

  4. 用layer-list实现图片旋转叠加、错位叠加、阴影、按钮指示灯

    先来看看一个简单的文件: <?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:a ...

  5. Java 集合细节(二):asList 的缺陷

    在实际开发过程中我们经常使用 asList 讲数组转换为 List,这个方法使用起来非常方便,但是 asList 方法存在几个缺陷: 一.避免使用基本数据类型数组转换为列表 使用 8 个基本类型数组转 ...

  6. C#程序集Assembly学习随笔(第一版)_AX

    ①什么是程序集?可以把程序集简单理解为你的.NET项目在编译后生成的*.exe或*.dll文件.嗯,这个确实简单了些,但我是这么理解的.详细:http://blog.csdn.net/sws8327/ ...

  7. vanilla

    In information technology, vanilla (pronounced vah-NIHL-uh ) is an adjective meaning plain or basic. ...

  8. pchar,pwidechar,pansichar作为返回参数时内存访问错误

    function Test:pachr: var   str: string; begin   str := 'Test Char';   result:=pchar(str); end; 上面的Te ...

  9. Linux中如何查看文件夹的大小

    直接查看当前文件夹的大小: du –sh 只看文件夹的名字里包含某字符串的子文件夹的大小: du –h –d 1 | grep "BACKEND" 我的linux系统被阉割的比较厉 ...

  10. SQL SERVER CXPACKET-Parallelism Wait Type 的惯用解决方案

    最近我的两个库出现,出现较多的CXPACKET等待,在网上找了一下资料.其中有篇一个SQL Server专栏作家的文章不错,也解决了我的一些疑问,就翻译在这里. 翻译整理仅用于传播资讯之目的. 原文出 ...