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

代码

 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 = 0; 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 = 0; i < 4; i++) {
26         int newx = x + addx[i];
27         int newy = y + addy[i];
28
29         if (newx >= 0 && newx < grid.size() && newy >= 0 && newy < grid[0].size()
30             && grid[newx][newy] == 0)
31         {
32             int idx = x * grid[0].size() + y;
33             int anotherIdx = newx * grid[0].size() + newy;
34             merge(idx, anotherIdx);
35         }
36     }
37 }
38
39 int closedIsland(vector<vector<int>>& grid) {
40     MAX_NUM = 110;
41     field = vector<vector<int>>(MAX_NUM, vector<int>(MAX_NUM));
42     fa = vector<int>(MAX_NUM*MAX_NUM + 1);
43     init(MAX_NUM*MAX_NUM);
44     addx = vector<int>{ 1,-1,0,0 };
45     addy = vector<int>{ 0,0,-1,1 };
46     for (int i = 0; i < grid.size(); i++) {
47         for (int j = 0; j < grid[0].size(); j++) {
48             if (grid[i][j] == 0) {
49                 check(i, j, grid);
50             }
51         }
52     }
53
54     set<int> s;
55
56     for (int i = 0; i < grid.size(); i++) {
57         for (int j = 0; j < grid[0].size(); j++) {
58             if (grid[i][j] == 0) {
59                 int idx = i * grid[0].size() + j;
60                 s.insert(get(idx));
61             }
62         }
63     }
64
65     //从统计的并查集 删除与边沿有关的陆地
66     for (int i = 0; i < grid.size(); i++) {
67         for (int j = 0; j < grid[0].size(); j++) {
68             if (grid[i][j] == 0 && (i == 0 || i == grid.size() - 1 || j == 0 || j == grid[0].size() - 1)) {
69                 int idx = i * grid[0].size() + j;
70                 s.erase(get(idx));
71             }
72         }
73     }
74
75     return s.size();
76 }
77
78 };

原文地址:https://www.cnblogs.com/itdef/p/11830193.html

时间: 2024-10-28 20:38:34

LEETCODE 5257. 统计封闭岛屿的数目 Number of Closed Islands的相关文章

【Leetcode 深搜】统计封闭岛屿的数目(1254)

题目 有一个二维矩阵 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

[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) connected 4-directionally (horizontal or vertical.) You may assume all four edges of the grid are surrounded by water. Count the number of distinct island

[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) connected 4-directionally (horizontal or vertical.) You may assume all four edges of the grid are surrounded by water. Count the number of distinct island

leetcode 刷题之路 84 Single Number II

Given an array of integers, every element appears three times except for one. Find that single one. 给定一个包含n个整数的数组,除了一个数出现一次外所有的整数均出现三次,找出这个只出现一次的整数. 思路,根据数组构成的特点可知,数组中所有数的某一位上1的个数总和为3*n或者3*n+1. 当只出现一次的整数该位为0时,总和为3*n,当只出现一次的整数该位为1时,总和为3*n+1. 因此我们可以计算数

[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) connected 4-directionally (horizontal or vertical.) You may assume all four edges of the grid are surrounded by water. Count the number of distinct island

694. Number of Distinct Islands - Medium

Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) connected 4-directionally (horizontal or vertical.) You may assume all four edges of the grid are surrounded by water. Count the number of distinct island

[LC] 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) connected 4-directionally (horizontal or vertical.) You may assume all four edges of the grid are surrounded by water. Count the number of distinct island

&amp;lt;LeetCode OJ&amp;gt; 26 / 264 / 313 Ugly Number (I / II / III)

Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 6, 8 are ugly while 14 is not ugly since it includes another prime factor 7. Note that 1 is ty

【LeetCode】【Python题解】Single Number &amp; Maximum Depth of Binary Tree

今天做了三道LeetCode上的简单题目,每道题都是用c++和Python两种语言写的,因为c++版的代码网上比较多,所以就只分享一下Python的代码吧,刚学完Python的基本语法,做做LeetCode的题目还是不错的,对以后找工作面试也有帮助! 刚开始就从AC率最高的入手吧! 1.Given an array of integers, every element appears twice except for one. Find that single one. Note: Your a