当前位置:网站首页>LeetCode_ DFS_ Medium_ 1254. Count the number of closed islands
LeetCode_ DFS_ Medium_ 1254. Count the number of closed islands
2022-04-23 08:52:00 【A scoop of Jianghu me ups and downs】
1. subject
Two dimensional matrix grid from 0 ( land ) and 1( water ) form . The island is made up of the largest 4 Connected in two directions 0 Group composed of , The closed island is a completely 1 Surround ( Left 、 On 、 Right 、 Next ) The island of .
Please return to the number of closed Islands .
Example 1:

Input :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]]
Output :2
explain :
The islands in the gray area are closed Islands , Because the island is completely surrounded by water ( Namely be 1 The area surrounds ).
Example 2:

Input :grid = [[0,0,1,0,0],[0,1,0,1,0],[0,1,1,1,0]]
Output :1
Example 3:
Input :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]]
Output :2
Tips :
1 <= grid.length, grid[0].length <= 100
0 <= grid[i][j] <=1
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/number-of-closed-islands
2. Ideas
(1)DFS
The problem is actually LeetCode_DFS_ secondary _200. Number of Islands This deformation problem , Just remove... From its foundation grid The islands that are not closed around the central bank can be .
3. Code implementation (Java)
// Ideas 1————DFS
class Solution {
/* take 200. The number of islands on the side of the island is deleted , You can get the number of closed Islands But here's the thing , In this question '0' Representing land ,'1' It stands for sea water */
public int closedIsland(int[][] grid) {
//res Keep the number of islands closed , The initial value is 0
int res = 0;
int m = grid.length;
int n = grid[0].length;
for (int i = 0; i < m; i++) {
// hold grid The island on the left in the middle was submerged
dfs(grid, i, 0);
// hold grid The island on the right in the middle was submerged
dfs(grid, i, n - 1);
}
for (int i = 0; i < n; i++) {
// hold grid The island above the middle was submerged
dfs(grid, 0, i);
// hold grid The lower island in the middle was submerged
dfs(grid, m - 1, i);
}
// Traverse grid, The remaining islands are closed
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (grid[i][j] == 0) {
// Find an island
res++;
// Use DFS Submerge the island ( Including the adjacent land up, down, left and right )
dfs(grid, i, j);
}
}
}
return res;
}
// From the position (i, j) Start , Turn the land adjacent to it into sea water
public void dfs(int[][] grid, int i, int j) {
int m = grid.length;
int n = grid[0].length;
// Judge boundary condition
if (i < 0 || j < 0 || i >= m || j >= n) {
return;
}
if (grid[i][j] == 1) {
// The current position is already sea water , Then return directly
return;
}
// Change the current position to sea water , Equivalent to using an array visited Record the traversed nodes
grid[i][j] = 1;
// At the same time, it also submerges the land up, down, left and right
dfs(grid, i - 1, j);
dfs(grid, i + 1, j);
dfs(grid, i, j - 1);
dfs(grid, i, j + 1);
}
}
版权声明
本文为[A scoop of Jianghu me ups and downs]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230848013554.html
边栏推荐
- Test your machine learning pipeline
- Noyer électronique stm32 Introduction à l'Internet des objets 30 étapes notes I. différences entre la Bibliothèque Hal et la Bibliothèque standard
- 洋桃电子STM32物联网入门30步笔记三、CubeMX图形化编程、设置开发板上的IO口
- 请提前布局 Star Trek突破链游全新玩法,市场热度持续高涨
- Please arrange star trek in advance to break through the new playing method of chain tour, and the market heat continues to rise
- 请问中衍期货安全靠谱吗?
- Study notes of deep learning (8)
- 2021李宏毅机器学习之Adaptive Learning Rate
- Brief steps to build a website / application using flash and H5
- Whether the same binary search tree (25 points)
猜你喜欢

企业微信应用授权/静默登录

OneFlow學習筆記:從Functor到OpExprInterpreter

使用flask和h5搭建网站/应用的简要步骤

汇编语言与逆向工程 栈溢出漏洞逆向分析实验报告

Chris LATTNER, father of llvm: the golden age of compilers

Noyer électronique stm32 Introduction à l'Internet des objets 30 étapes notes I. différences entre la Bibliothèque Hal et la Bibliothèque standard

PCTP考试经验分享

Idea is configured to connect to the remote database mysql, or Navicat fails to connect to the remote database (solved)

Virtual online exhibition - Online VR exhibition hall realizes 24h immersive exhibition viewing

Summary of solid problems
随机推荐
Complete binary search tree (30 points)
2022-04-22 openebs cloud native storage
Multi view depth estimation by fusing single view depth probability with multi view geometry
汇编语言与逆向工程 栈溢出漏洞逆向分析实验报告
Flink同时读取mysql与pgsql程序会卡住且没有日志
深度学习框架中的自动微分及高阶导数
【精品】利用动态代理实现事务统一管理 二
计算神经网络推理时间的正确方法
Harbor企业级镜像管理系统实战
Idea import commons-logging-1.2 Jar package
Kubernetes如何使用harbor拉去私有镜像
L2-3 romantic silhouette (25 points)
Consensus Token:web3.0生态流量的超级入口
扣缴义务人
Basic usage of synchronized locks
Test your machine learning pipeline
STM32 uses Hal library. The overall structure and function principle are introduced
Automatic differentiation and higher order derivative in deep learning framework
调包求得每个样本的k个邻居
Yangtao electronic STM32 Internet of things introduction 30 steps notes 1. The difference between Hal library and standard library