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 ...
随机推荐
- MySQL 排错-解决MySQL非聚合列未包含在GROUP BY子句报错问题
排错-解决MySQL非聚合列未包含在GROUP BY子句报错问题 By:授客 QQ:1033553122 测试环境 win10 MySQL 5.7 问题描述: 执行类似以下mysql查询, SEL ...
- 详解Python函数参数定义及传参(必备参数、关键字参数、默认可省略参数、可变不定长参数、*args、**kwargs)
详解Python函数参数定义及传参(必备参数.关键字参数.默认可省略参数.可变不定长参数.*args.**kwargs) Python函数参数传参的种类 Python中函数参数定义及调用函数时传参 ...
- Linux(Centos7)下Mysql的安装
1.1 查看mysql的安装路径: [root@bogon ~]# whereis mysql mysql: /usr/bin/mysql /usr/lib/mysql /usr/share/mysq ...
- 经典案例:如何优化Oracle使用DBlink的SQL语句
转自 https://blog.csdn.net/Enmotech/article/details/78788083 作者介绍 赵全文 就职于太极计算机股份有限公司,在中央电化教育馆做Oracle D ...
- vue 脚手架
Vue 脚手架的基本用法 1. 基于 3.X 版本的脚手架 创建vue项目 命令行(CLI) 的方式创建 vue 项目 vue create my-project 图形化界面(GUI) 的方式创建 v ...
- python 实现 PC 客户端自动化快速入门:pywinauto !
本文转载自:http://www.lemfix.com/topics/420 一.前言 我们柠檬班的小可爱,在学完我们柠檬班自动化的课程之后,就掌握了接口自动化,web自动化,app自动化,这些工 ...
- 「Shimo使用指南」mac支持pptp协议的小软件
Mac的好多小伙伴在访问网络设备时觉得远程连接不方便,例如ssh,***登陆都不是很方便,后来又安装了open*** forMac.ISSH等客户端,使用后发现不是很稳定,断线后很久都无法连接等缺点, ...
- 腾讯云服务器ubuntu18.04部署禅道系统
踩了不少坑,记录一下. 基于ubuntu18.04 一开始按照网上的攻略下载安装包 ZenTaoPMS.9.8.3.zbox_64.tar.gz,通过FileZilla传到linux的/opt下面,解 ...
- 2019-2020-1 20199305《Linux内核原理与分析》第八周作业
可执行程序的工作原理 (一)ELF目标文件 (1)什么是ELF? 这里先提一个常见的名词"目标文件",是指编译器生成的文件.ELF(Executable and Linkable ...
- [考试反思]1111csp-s模拟测试110:三思
题目名是为了照应3天的倒计时(我才不会说是因为我考场又摸鱼了) 在OJ上得到了295的好成绩,但是本地评测没有O2掉了10分. 总体来说还可以.T1全场切,T2半场切,T3纯暴力不卡常都有95... ...