描述

给一个01矩阵,求不同的岛屿的个数。

0代表海,1代表岛,如果两个1相邻,那么这两个1属于同一个岛。我们只考虑上下左右为相邻。

样例

在矩阵:

[
[1, 1, 0, 0, 0],
[0, 1, 0, 0, 1],
[0, 0, 0, 1, 1],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 1]
]

中有 3 个岛。

package com.ossez.lang.tutorial.tests.lintcode;

import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory; /**
* <p>
* 433
* <ul>
* <li>@see <a href=
* "https://www.cwiki.us/display/ITCLASSIFICATION/Number+of+Islands">https://www.cwiki.us/display/ITCLASSIFICATION/Number+of+Islands</a>
* <li>@see<a href="https://www.lintcode.com/problem/number-of-islands/">https://www.lintcode.com/problem/number-of-islands/</a>
* </ul>
* </p>
*
* @author YuCheng
*
*/
public class LintCode0433NumIslandsTest { private final static Logger logger = LoggerFactory.getLogger(LintCode0433NumIslandsTest.class); /**
*
*/
@Test
public void testMain() {
logger.debug("BEGIN");
// INIT GRID
boolean[][] grid = { { true, true, false, false, false }, { false, true, false, false, true }, { false, false, false, true, true },
{ false, false, false, false, false }, { false, false, false, false, true } }; // NULL CHECK
if (grid.length == 0 || grid[0].length == 0) {
System.out.println("NULL");
// return 0;
} // GET SIZE
int n = grid.length;
int m = grid[0].length; // ARRAY FOR VISITED LOG
boolean[][] visited = new boolean[n][m]; int count = 0; // LOOP FOR GRID
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (grid[i][j] && !visited[i][j]) {
numIslandsDFS(grid, visited, i, j);
count++;
}
}
} System.out.println(count); } /**
*
* @param grid
* @param visited
* @param x
* @param y
*/
public void numIslandsDFS(boolean[][] grid, boolean[][] visited, int x, int y) {
if (x < 0 || x >= grid.length) {
return;
} if (y < 0 || y >= grid[0].length) {
return;
} if (grid[x][y] != true || visited[x][y]) {
return;
} visited[x][y] = true; // Recursive call
numIslandsDFS(grid, visited, x - 1, y);
numIslandsDFS(grid, visited, x + 1, y);
numIslandsDFS(grid, visited, x, y - 1);
numIslandsDFS(grid, visited, x, y + 1); }
}

点评

本质是求矩阵中连续区域的个数,很容易想到需要用深度优先搜索 DFS 来解,我们需要建立一个 visited 数组用来记录某个位置是否被访问过,对于一个为 true 且未被访问过的位置,我们递归进入其上下左右位置上为 true 的数,将其 visited 对应值赋为 true,继续进入其所有相连的邻位置,这样可以将这个连通区域所有的数找出来,并将其对应的 visited 中的值赋 true,找完次区域后,我们将结果 res 自增 1,然后我们在继续找下一个为 true 且未被访问过的位置,以此类推直至遍历完整个原数组即可得到最终结果。

这里需要有一个递归的调用。在递归调用之前需要进行判断是否超出边际,如果超出边际的话,就要跳出循环。

在一个节点进行遍历的时候,需要在递归调用的时候,同时针对这个节点搜索上下左右 4 个节点,如果找到需要了满足条件的 true,就继续查找,如果没有找到就退出。在这个过程的时候,需要将访问过的节点保存到访问控制的 2 维数组中。以便于在下次查找的时候跳过这个节点。

https://www.cwiki.us/display/ITCLASSIFICATION/Number+of+Islands

[LintCode] Number of Islands(岛屿个数)的更多相关文章

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

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

  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. [LeetCode] 0200. Number of Islands 岛屿的个数

    题目 Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is su ...

  4. [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 ...

  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. 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 ...

  7. 【LeetCode】200. Number of Islands 岛屿数量

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://le ...

  8. lintcode:Number of Islands 岛屿的个数

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

  9. 200 Number of Islands 岛屿的个数

    给定 '1'(陆地)和 '0'(水)的二维网格图,计算岛屿的数量.一个岛被水包围,并且通过水平或垂直连接相邻的陆地而形成.你可以假设网格的四个边均被水包围.示例 1:11110110101100000 ...

随机推荐

  1. Python3 tkinter基础 Canvas create_rectangle 画虚边的矩形 create_oval 画椭圆形 圆形

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  2. tp剩余未验证内容-5

    经过实践, ie678是不能正确显示解析bs的,所以要用ff和chrome浏览器. page-header类是有特殊样式的 在标题下有一条浅色的细线条,源代码中有: border-bottom: 1p ...

  3. LIS|计蒜客2019蓝桥杯省赛 B 组模拟赛(一)

    #include <bits/stdc++.h> using namespace std; const int N = 1e5 + 9; int f[N], a[N]; int n; // ...

  4. 论文笔记:Attention Is All You Need

    Attention Is All You Need 2018-04-17 10:35:25  Paper:http://papers.nips.cc/paper/7181-attention-is-a ...

  5. ffmpeg 下载安装和简单应用

    一.ffmpeg下载 先到http://ffmpeg.org/下载ffmpeg安装文件 二.ffmpeg安装 1.解压下载完的ffmpeg-20190319-f8075b2-win64-shared. ...

  6. 嵌入式Linux要学哪些东西?你真的造吗?

    嵌入式Linux要学哪些?一些人总在寻思,怕走了弯路,又怕学的东西离企业需求远.那么今天就请华清远见高级讲师曹大神告诉你,9点浅析嵌入式学习步骤.下面是他本人亲笔. 1.要学习Linux,首先要会用, ...

  7. Linux安装python3.7

    1.下载与解压 先到python官网: https://www.python.org/downloads/release/python-371/](https://www.python.org/dow ...

  8. python学习 day06打卡

    今天学习的主要内容是: 一,小数据池 代码块的概念 python程序是由代码块构成的,一个代码块的文本作为python程序执行的单元. 代码块:一个模块,一个函数,一个类,甚至每一个command命令 ...

  9. Appium典型问题处理

    1. http://ask.testfan.cn/article/902 Appium 服务端安装-windows2. http://ask.testfan.cn/article/1078 最新版本a ...

  10. JAVA读取CSV文件到MySQL数据库中

    maven项目pom配置: <dependency> <groupId>net.sourceforge.javacsv</groupId> <artifact ...