分析:经典连通分量问题

图:

  节点:所有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. 基于设备树的TQ2440的中断(2)

    下面以按键中断为例看看基于设备数的中断的用法: 设备树: tq2440_key { compatible = "tq2440,key"; interrupt-parent = &l ...

  2. AngularJS报错:[$compile:tpload]

    页面中有: <div ng-view></div> 这里会根据不同的路由设置选择不同模版下的内容. 如果把AngularJS网站项目放在包含中文字的目录中,会报以上的错. 解决 ...

  3. 在Visual Studio中使用用例图描述系统与参与者间的关系

    "用例图"用来描述谁用系统,用系统做什么.用例图不涉及使用细节,只用来描述使用人员和系统的关系,也不涉及行动的顺序.一起来体验. 使用Visual Studio 2012创建解决方 ...

  4. delphi services允许跨域访问

    delphi services允许跨域访问 unit WebModuleUnit1; procedure TWebModule1.WebModule1DefaultHandlerAction(Send ...

  5. Odoo8出现Internal Server Error的解决办法之一

    转载地址:http://blog.sina.com.cn/s/blog_7cb52fa80102vaf3.html     问题: 不知怎么回事,我的Odoo8出错了,重装也一样出错信息如下 Inte ...

  6. IOS开发常用宏定义

    //release屏蔽NSLog//放在.pch文件里#ifdef DEBUG #else#define NSLog(...) {};#endif //G.C.D#define BACK(block) ...

  7. SharePoint 2016 工作流报错“未安装应用程序管理共享服务代理”

    前言 最近为SharePoint 2016环境,配置了状态机工作流,然后,用spd创建的时候可以保存,但是发布的时候报错,经过排查解决了问题,记录一下. 报错截图 下面是SharePoint Desi ...

  8. 能添加图标的label

    能添加图标的label 效果 源码 https://github.com/YouXianMing/UI-Component-Collection 中的 IconEdgeInsetsLabel // / ...

  9. Easyui 页面设置加载完成之后,满屏

    js文件: if(top.location!=self.location){ top.location.href=self.location; }

  10. 我的Java学习推荐书目

    一直有这么个想法,列一下我个人认为在学习和使用Java过程中可以推荐一读的书籍,给初学者或者想深入的朋友一些建议,帮助成长.推荐的的都是我自己读过,也会推荐一些朋友读过并且口碑不错的书籍. 一.基础类 ...