地址 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. 4、netty第三个例子,建立一个tcp的聊天的程序

    代码基于第二个例子,支持多客户端的连接,在线聊天. 主要思路: 连接建立时,在服务器端,保存channel 对象,当有新的客户端加入时,遍历保存的channel集合,向其他客户端发送加入消息. 当一个 ...

  2. IDEA快捷键用法

    1.Ctrl+滑动滚轮调节窗口显示大小(需要设置之后方可) File->Settings->Editor->General->Change fontsize with Ctrl ...

  3. 利用Haproxy搭建 HTTP 请求走私(Request smuggling)环境

    Haproxy 介绍 HAProxy是一个使用C语言编写的自由及开放源代码软件,其提供高可用性.负载均衡,以及基于TCP和HTTP的应用程序代理. 请求走私(Request smuggling)概念证 ...

  4. mysql数据库基础SQL语句总结篇

    常用的sql增删改查语句 创建数据库:create database db_name character set utf8;删除数据库:drop database db_name;切换数据库:use ...

  5. koa2跨域模块koa2-cors

    之前写了一个api在小程序里调用,但是我不想每次都打开小程序,所以想写一个简单的网页,但是遇到CORB的问题: 经尝试,jsonp等都没起作用,由于我后台是koa写的,发现koa2-cors库可以解决 ...

  6. Flink概述| 配置

    流处理技术的演变 在开源世界里,Apache Storm项目是流处理的先锋.Storm提供了低延迟的流处理,但是它为实时性付出了一些代价:很难实现高吞吐,并且其正确性没能达到通常所需的水平,换句话说, ...

  7. 使用python - selenium模拟登陆b站

    思路 输入用户名密码点击登陆 获取验证码的原始图片与有缺口的图片 找出两张图片的缺口起始处 拖动碎片 功能代码段 # 使用到的库 from selenium import webdriver from ...

  8. SQLServer之数据库表转化为实体类【带注释】

    1.在开发过程中,有时候需要将数据库表转化为实体类.手敲除了不方便,还容易出错.本着DRY+懒人原则,参考了一位老司机的博客[见底部],并在其基础上进行了优化.[原先是不带注释的] DECLARE @ ...

  9. 【ZJOI 2014】力

    Problem Description 给出 \(n\) 个数 \(q_i\),给出 \(F_j\) 的定义如下: \[F_j=\sum_{i<j} \frac{q_iq_j}{(i-j)^2} ...

  10. [WPF 自定义控件]使用WindowChrome自定义RibbonWindow

    1. 为什么要自定义RibbonWindow 自定义Window有可能是设计或功能上的要求,可以是非必要的,而自定义RibbonWindow则不一样: 如果程序使用了自定义样式的Window,为了统一 ...