LEETCODE 1254 统计封闭岛屿的数目 Number of Closed Islands
地址 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的更多相关文章
- Leetcode 1254. 统计封闭岛屿的数目
题目: 有一个二维矩阵 grid ,每个位置要么是陆地(记号为 0 )要么是水域(记号为 1 ). 我们从一块陆地出发,每次可以往上下左右 4 个方向相邻区域走,能走到的所有陆地区域,我们将其称为一座 ...
- 【leetcode】1254. Number of Closed Islands
题目如下: Given a 2D grid consists of 0s (land) and 1s (water). An island is a maximal 4-directionally ...
- [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 ...
- [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 ...
- Leetcode之深度优先搜索(DFS)专题-200. 岛屿数量(Number of Islands)
Leetcode之深度优先搜索(DFS)专题-200. 岛屿数量(Number of Islands) 深度优先搜索的解题详细介绍,点击 给定一个由 '1'(陆地)和 '0'(水)组成的的二维网格,计 ...
- [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 ...
- [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 ...
- 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 ...
- [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 ...
随机推荐
- iOS技术博客
iOS 技术提高 原文 http://blog.devtang.com/2014/07/27/ios-levelup-tips/# 1.阅读博客 美团技术博客 https://tech.meituan ...
- BIM工程信息管理系统-EF实体框架数据操作基类
EF实体框架数据操作基类主要是规范增.改.查.分页.Lambda表达式条件处理,以及异步操作等特性,这样能够尽可能的符合基类这个特殊类的定义,实现功能接口的最大化重用和统一. 1.程序代码 /// & ...
- 追踪SQL Server执行delete操作时候不同锁申请与释放的过程
一直以为很了解sqlserver的加锁过程,在分析一些特殊情况下的死锁之后,尤其是并发单表操作发生的死锁,对于加解锁的过程,有了一些重新的认识,之前的知识还是有一些盲区在里面的.delete加锁与解锁 ...
- ORA-27468: ""."" is locked by another process
You have a scheduler job that generated an error. When the error occurred, you attempted to disable ...
- 经典案例:如何优化Oracle使用DBlink的SQL语句
转自 https://blog.csdn.net/Enmotech/article/details/78788083 作者介绍 赵全文 就职于太极计算机股份有限公司,在中央电化教育馆做Oracle D ...
- python-将一个列表切分成多个小列表
list是python中较为常见的数据类型,它是一个可迭代对象,迭代是什么?简单的可以理解成:一个可以被for循环遍历的对象 今天拿到一个类似这样的list list_info = ['name zh ...
- ReactNative: 使用尺寸类Dimensions获取屏幕尺寸
一.简介 在前面创建使用组件时,虽然使用的都是伸缩盒子布局,但是很少使用宽高来进行绝对定位.在iOS中可以通过UIScreen控件获取当前屏幕的宽高,同样地,在RN中提供了一个尺寸组件Dimensio ...
- IT兄弟连 HTML5教程 CSS3揭秘 小结及习题
小结 CSS3对于开发者来说,给web应用带来了更多的可能性,极大提高了开发效率.CSS3在选择器上的支持可谓是丰富多彩,使得我们能够灵活的控制样式,而不必为元素进行规范化的命名.CSS3支持的动画类 ...
- CH-0304 IncDec Sequence
0304 IncDec Sequence 0x00「基本算法」例题 描述 给定一个长度为 n(n≤10^5 ) 的数列 {a_1,a_2,…,a_n},每次可以选择一个区间 [l,r],使下标在这个区 ...
- 【Hash一致性算法】什么是Hash一致性算法
目录 1. 一致性Hash算法简介 环形Hash空间 把数据通过一定的hash算法处理后映射到环上 将机器通过hash算法映射到环上 机器的删除与添加 平衡性 本文转载自博客 1. 一致性Hash算法 ...