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 ...
随机推荐
- HALCON数据类型和C#对应数据类型的对比
摘要:HALCON数据类型:Iconic Variables(图形变量).Control Variables(控制变量).在C#中,图形变量用HObject声明,控制变量用HTuple声明.(halc ...
- django nginx 504 time-out 错误
报错: 分析 nginx和uwsgi整合时有三个参数可以用于设置超时时间: 1.uwsgi_connect_timeout: 默认60秒,与uwsgi-server连接的超时时间,该值不能超过75秒. ...
- VO(视图模型) 与 DTO(数据传输对象)的区别
目录 VO(视图模型) 与 DTO(数据传输对象)的区别 1.VO与DTO概念 2.VO 视图模型的必要性与解耦 2.1 视图模型 2.2 视图模型存在的必要性 2.3 视图模型的解耦 3.DTO 存 ...
- JavaScript 自定义html元素鼠标右键菜单
自定义html元素鼠标右键菜单 实现思路 在触发contextmenu事件时,取消默认行为(也就是阻止浏览器显示自带的菜单),获取右键事件对象,来确定鼠标的点击位置,作为显示菜单的left和top值 ...
- mongo [initandlisten] exception in initAndListen: 98 Unable to create/open lock file: /data/db/mongod.lock errno:13 Permission denied Is a mongod instance already running?, terminating 2019-09-23T16:
解决方法: 加权 sudo chmod -Rf 777 /data/db
- solo升级以及自动化更新的方法
使用solo过程总涉及到更新问题,所以就在这里把solo更新的方法总结一下.希望能给小伙伴们一些帮助.如何选择更新方法主要是跟你的部署方式有关,如果你是通过 docker方式进行部署,那么你可以还可以 ...
- centos8 安装 nginx
http://nginx.org/ NGINX官网 创建文件夹mkdir nginx进入创建的文件夹 根据自己需要下载合适版本 通过 wget http://nginx.org/download/ng ...
- com.mysql.cj.exceptions.DataReadException: Zero date value prohibited
com.mysql.cj.exceptions.DataReadException: Zero date value prohibited at com.mysql.cj.result.SqlTime ...
- 深入浅出xpath轴定位
在web自动化里面经常要用到定位,常用的八种定位方式中我最喜欢xpath定位,功能很强大.结合它里面的文本定位.模糊定位.逻辑定位等,基本能搞定所有的元素定位问题. 今天要讨论的是xpath的另一种比 ...
- Java之Date类
Date类的概述 java.util.Date类 表示特定的瞬间,精确到毫秒.毫秒:千分之一秒 1000毫秒=1秒.特定的瞬间:一个时间点,一刹那时间. 常用构造方法 public Date():分配 ...