地址 https://leetcode-cn.com/contest/weekly-contest-162/problems/number-of-closed-islands/

有一个二维矩阵 grid ,每个位置要么是陆地(记号为 0 )要么是水域(记号为 1 )。

我们从一块陆地出发,每次可以往上下左右 4 个方向相邻区域走,能走到的所有陆地区域,我们将其称为一座「岛屿」。

如果一座岛屿 完全 由水域包围,即陆地边缘上下左右所有相邻区域都是水域,那么我们将其称为 「封闭岛屿」。

请返回封闭岛屿的数目。

示例1

输入:grid = [[1,1,1,1,1,1,1,0],[1,0,0,0,0,1,1,0],[1,0,1,0,1,1,1,0],[1,0,0,0,0,1,0,1],[1,1,1,1,1,1,1,0]]
输出:2
解释:
灰色区域的岛屿是封闭岛屿,因为这座岛屿完全被水域包围(即被 1 区域包围)。

示例2

输入:grid = [[0,0,1,0,0],[0,1,0,1,0],[0,1,1,1,0]]
输出:1

示例3

输入:grid = [[1,1,1,1,1,1,1],
[1,0,0,0,0,0,1],
[1,0,1,1,1,0,1],
[1,0,1,0,1,0,1],
[1,0,1,1,1,0,1],
[1,0,0,0,0,0,1],
[1,1,1,1,1,1,1]]
输出:2

题解 基本都是使用DFS 查找是否与边界的0有所连通

这里提供另一种思路 并查集

检测所有标记为陆地的0 进行归并  然后将与边界连通的陆地集合删除 最后留下的就是封闭岛屿

代码

 class Solution {
public: int MAX_NUM;
vector<vector<int>> field;
vector<int> fa;
vector<int> addx;
vector<int> addy;
void init(int n)
{
for (int i = ; i <= n; i++)
fa[i] = i;
}
int get(int x)
{
return fa[x] == x ? x : fa[x] = get(fa[x]);//路径压缩,防止链式结构
}
void merge(int x, int y)
{
fa[get(x)] = get(y);
}
//================================================
void check(int x, int y, vector<vector<int>>& grid)
{
for (int i = ; i < ; i++) {
int newx = x + addx[i];
int newy = y + addy[i]; if (newx >= && newx < grid.size() && newy >= && newy < grid[].size()
&& grid[newx][newy] == )
{
int idx = x * grid[].size() + y;
int anotherIdx = newx * grid[].size() + newy;
merge(idx, anotherIdx);
}
}
} int closedIsland(vector<vector<int>>& grid) {
MAX_NUM = ;
field = vector<vector<int>>(MAX_NUM, vector<int>(MAX_NUM));
fa = vector<int>(MAX_NUM*MAX_NUM + );
init(MAX_NUM*MAX_NUM);
addx = vector<int>{ ,-,, };
addy = vector<int>{ ,,-, };
for (int i = ; i < grid.size(); i++) {
for (int j = ; j < grid[].size(); j++) {
if (grid[i][j] == ) {
check(i, j, grid);
}
}
} set<int> s; for (int i = ; i < grid.size(); i++) {
for (int j = ; j < grid[].size(); j++) {
if (grid[i][j] == ) {
int idx = i * grid[].size() + j;
s.insert(get(idx));
}
}
} //从统计的并查集 删除与边沿有关的陆地
for (int i = ; i < grid.size(); i++) {
for (int j = ; j < grid[].size(); j++) {
if (grid[i][j] == && (i == || i == grid.size() - || j == || j == grid[].size() - )) {
int idx = i * grid[].size() + j;
s.erase(get(idx));
}
}
} return s.size();
} };

LEETCODE 1254 统计封闭岛屿的数目 Number of Closed Islands的更多相关文章

  1. Leetcode 1254. 统计封闭岛屿的数目

    题目: 有一个二维矩阵 grid ,每个位置要么是陆地(记号为 0 )要么是水域(记号为 1 ). 我们从一块陆地出发,每次可以往上下左右 4 个方向相邻区域走,能走到的所有陆地区域,我们将其称为一座 ...

  2. 【leetcode】1254. Number of Closed Islands

    题目如下: Given a 2D grid consists of 0s (land) and 1s (water).  An island is a maximal 4-directionally ...

  3. [LeetCode] Number of Distinct Islands II 不同岛屿的个数之二

    Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) conn ...

  4. [LeetCode] Number of Distinct Islands 不同岛屿的个数

    Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) conn ...

  5. Leetcode之深度优先搜索(DFS)专题-200. 岛屿数量(Number of Islands)

    Leetcode之深度优先搜索(DFS)专题-200. 岛屿数量(Number of Islands) 深度优先搜索的解题详细介绍,点击 给定一个由 '1'(陆地)和 '0'(水)组成的的二维网格,计 ...

  6. [LeetCode] 711. Number of Distinct Islands II 不同岛屿的个数之二

    Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) conn ...

  7. [LeetCode] 694. Number of Distinct Islands 不同岛屿的个数

    Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) conn ...

  8. 694. Number of Distinct Islands 形状不同的岛屿数量

    [抄题]: Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land ...

  9. [leetcode]694. Number of Distinct Islands你究竟有几个异小岛?

    Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) conn ...

随机推荐

  1. C#版本与.NET版本对应关系以及各版本的特性

    C#版本 .NET版本 发布日期 特性 C# 1.0 .NET Framework 1.0 2002-02-13 委托.事件 C# 1.1 .NET Framework 1.1 2003-04-24 ...

  2. 松软科技web课堂:JavaScript 布尔(逻辑)

    JavaScript 布尔(逻辑)代表两个值之一:true 或 false. 布尔值 通常,在编程中,您会需要只能有两个值之一的数据类型,比如 YES / NO ON / OFF TRUE / FAL ...

  3. Vue学习笔记Day2

    1.mustache语法 如何将data中的文本数据插入到HTML中? 通过使用mustache语法(也就是双大括号),将data中的变量名插入到HTML元素中,显示在页面上. 如下图:并且数据是响应 ...

  4. Django 执行 makemigrations 显示 No changes detected in app

    在Django项目配置一下多数据库,但是运行 makemigrations 执行不正常 $ python manage.py makemigrations polls No changes detec ...

  5. 1、nio说明 和 对比bio

    nio和bio的区别 bio: 面向流的. 单向的. 阻塞的,这也是b这个的由来. nio: 面向块的.(buffer) 双向的. 非阻塞的.同步的编程方式.是一种select模型 nio编程的常规步 ...

  6. SQL Server如何找出一个表包含的页信息(Page)

    在SQL Server中,如何找到一张表或某个索引拥有那些页面(page)呢? 有时候,我们在分析和研究(例如,死锁分析)的时候还真有这样的需求,那么如何做呢? SQL Server 2012提供了一 ...

  7. [PHP] 接口增加recaptcha行为验证

    需要先翻墙创建一个谷歌账户和创建recaptcha验证的网站域名,获取到两个secrecthttps://www.google.com/recaptcha/admin 前端增加html和js代码,例如 ...

  8. Cocos2d-x 点击菜单按键居中放大(无需修改底层代码)

    建议转至该处阅读 https://www.zybuluo.com/tangyikejun/note/21953 配置环境:win7+Cocos2d-x.2.0.3+VS2012 目标读者:已经了解Co ...

  9. 游戏《Minecraft》IntelliJ下模组开发环境ForgeGradle的使用教程

    嗯,当你想搞个模组的时候,肯定需要用到FG. 就比如编译模组的时候. 很好,首先下载源码去. files.minecraftforge.net/ 然后打开命令行到源码目录下 执行命令~ Win:   ...

  10. libnl概述

    以下三个库都基于其核心库libnl: libnl-route:用于和Kernel中的Routing子系统交互. libnl-nf:用于和Kernel中的Netfilter子系统交互. libnl-ge ...