地址 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 进行归并  然后将与边界连通的陆地集合删除 最后留下的就是封闭岛屿

代码

  1. class Solution {
  2. public:
  3.  
  4. int MAX_NUM;
  5. vector<vector<int>> field;
  6. vector<int> fa;
  7. vector<int> addx;
  8. vector<int> addy;
  9. void init(int n)
  10. {
  11. for (int i = ; i <= n; i++)
  12. fa[i] = i;
  13. }
  14. int get(int x)
  15. {
  16. return fa[x] == x ? x : fa[x] = get(fa[x]);//路径压缩,防止链式结构
  17. }
  18. void merge(int x, int y)
  19. {
  20. fa[get(x)] = get(y);
  21. }
  22. //================================================
  23. void check(int x, int y, vector<vector<int>>& grid)
  24. {
  25. for (int i = ; i < ; i++) {
  26. int newx = x + addx[i];
  27. int newy = y + addy[i];
  28.  
  29. if (newx >= && newx < grid.size() && newy >= && newy < grid[].size()
  30. && grid[newx][newy] == )
  31. {
  32. int idx = x * grid[].size() + y;
  33. int anotherIdx = newx * grid[].size() + newy;
  34. merge(idx, anotherIdx);
  35. }
  36. }
  37. }
  38.  
  39. int closedIsland(vector<vector<int>>& grid) {
  40. MAX_NUM = ;
  41. field = vector<vector<int>>(MAX_NUM, vector<int>(MAX_NUM));
  42. fa = vector<int>(MAX_NUM*MAX_NUM + );
  43. init(MAX_NUM*MAX_NUM);
  44. addx = vector<int>{ ,-,, };
  45. addy = vector<int>{ ,,-, };
  46. for (int i = ; i < grid.size(); i++) {
  47. for (int j = ; j < grid[].size(); j++) {
  48. if (grid[i][j] == ) {
  49. check(i, j, grid);
  50. }
  51. }
  52. }
  53.  
  54. set<int> s;
  55.  
  56. for (int i = ; i < grid.size(); i++) {
  57. for (int j = ; j < grid[].size(); j++) {
  58. if (grid[i][j] == ) {
  59. int idx = i * grid[].size() + j;
  60. s.insert(get(idx));
  61. }
  62. }
  63. }
  64.  
  65. //从统计的并查集 删除与边沿有关的陆地
  66. for (int i = ; i < grid.size(); i++) {
  67. for (int j = ; j < grid[].size(); j++) {
  68. if (grid[i][j] == && (i == || i == grid.size() - || j == || j == grid[].size() - )) {
  69. int idx = i * grid[].size() + j;
  70. s.erase(get(idx));
  71. }
  72. }
  73. }
  74.  
  75. return s.size();
  76. }
  77.  
  78. };

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. MySQL 排错-解决MySQL非聚合列未包含在GROUP BY子句报错问题

    排错-解决MySQL非聚合列未包含在GROUP BY子句报错问题 By:授客 QQ:1033553122   测试环境 win10 MySQL 5.7 问题描述: 执行类似以下mysql查询, SEL ...

  2. 详解Python函数参数定义及传参(必备参数、关键字参数、默认可省略参数、可变不定长参数、*args、**kwargs)

    详解Python函数参数定义及传参(必备参数.关键字参数.默认可省略参数.可变不定长参数.*args.**kwargs) Python函数参数传参的种类   Python中函数参数定义及调用函数时传参 ...

  3. Linux(Centos7)下Mysql的安装

    1.1 查看mysql的安装路径: [root@bogon ~]# whereis mysql mysql: /usr/bin/mysql /usr/lib/mysql /usr/share/mysq ...

  4. 经典案例:如何优化Oracle使用DBlink的SQL语句

    转自 https://blog.csdn.net/Enmotech/article/details/78788083 作者介绍 赵全文 就职于太极计算机股份有限公司,在中央电化教育馆做Oracle D ...

  5. vue 脚手架

    Vue 脚手架的基本用法 1. 基于 3.X 版本的脚手架 创建vue项目 命令行(CLI) 的方式创建 vue 项目 vue create my-project 图形化界面(GUI) 的方式创建 v ...

  6. python 实现 PC 客户端自动化快速入门:pywinauto !

    本文转载自:http://www.lemfix.com/topics/420 一.前言 ​ 我们柠檬班的小可爱,在学完我们柠檬班自动化的课程之后,就掌握了接口自动化,web自动化,app自动化,这些工 ...

  7. 「Shimo使用指南」mac支持pptp协议的小软件

    Mac的好多小伙伴在访问网络设备时觉得远程连接不方便,例如ssh,***登陆都不是很方便,后来又安装了open*** forMac.ISSH等客户端,使用后发现不是很稳定,断线后很久都无法连接等缺点, ...

  8. 腾讯云服务器ubuntu18.04部署禅道系统

    踩了不少坑,记录一下. 基于ubuntu18.04 一开始按照网上的攻略下载安装包 ZenTaoPMS.9.8.3.zbox_64.tar.gz,通过FileZilla传到linux的/opt下面,解 ...

  9. 2019-2020-1 20199305《Linux内核原理与分析》第八周作业

    可执行程序的工作原理 (一)ELF目标文件 (1)什么是ELF? 这里先提一个常见的名词"目标文件",是指编译器生成的文件.ELF(Executable and Linkable ...

  10. [考试反思]1111csp-s模拟测试110:三思

    题目名是为了照应3天的倒计时(我才不会说是因为我考场又摸鱼了) 在OJ上得到了295的好成绩,但是本地评测没有O2掉了10分. 总体来说还可以.T1全场切,T2半场切,T3纯暴力不卡常都有95... ...