当前位置:网站首页>【LeetCode】1254-统计封闭岛屿的数量
【LeetCode】1254-统计封闭岛屿的数量
2022-07-02 12:09:00 【酥酥~】
二维矩阵 grid 由 0 (土地)和 1 (水)组成。岛是由最大的4个方向连通的 0 组成的群,封闭岛是一个 完全 由1包围(左、上、右、下)的岛。
请返回 封闭岛屿 的数目。
示例 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:
输入: g rid = [[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
提示:
- 1 <= grid.length, grid[0].length <= 100
- 0 <= grid[i][j] <=1
#广度优先遍历
class Solution(object):
def closedIsland(self, grid):
n = len(grid)
m = len(grid[0])
landcount = 0
for i in range(n):
for j in range(m):
if grid[i][j]==0:
val = 1
grid[i][j]=1
queue=[]
queue.append((i,j))
while queue:
x,y = queue[0]
queue.pop(0)
for xx,yy in [(x,y+1),(x,y-1),(x+1,y),(x-1,y)]:
if not 0<=xx<n or not 0<=yy<m:
val=0
elif grid[xx][yy]==0:
grid[xx][yy]=1
queue.append((xx,yy))
landcount+=val
return landcount
#深度优先遍历
class Solution(object):
def closedIsland(self, grid):
n = len(grid)
m = len(grid[0])
landcount = 0
for i in range(n):
for j in range(m):
if grid[i][j]==0:
val = 1
grid[i][j]=1
stack=[]
stack.append((i,j))
while stack:
x,y = stack[-1]
stack.pop()
for xx,yy in [(x,y+1),(x,y-1),(x+1,y),(x-1,y)]:
if not 0<=xx<n or not 0<=yy<m:
val=0
elif grid[xx][yy]==0:
grid[xx][yy]=1
stack.append((xx,yy))
landcount+=val
return landcount
边栏推荐
- LeetCode刷题——两整数之和#371#Medium
- PHP method to get the index value of the array item with the largest key value in the array
- 高考录取分数线爬取
- 4. Jctree related knowledge learning
- Mavn builds nexus private server
- 面对“缺芯”挑战,飞凌如何为客户产能提供稳定强大的保障?
- 2022 年辽宁省大学生数学建模A、B、C题(相关论文及模型程序代码网盘下载)
- Data analysis thinking analysis methods and business knowledge - business indicators
- How to choose a third-party software testing organization for automated acceptance testing of mobile applications
- Yolov5 code reproduction and server operation
猜你喜欢

Build your own semantic segmentation platform deeplabv3+

Let your HMI have more advantages. Fet-g2ld-c core board is a good choice

Data analysis thinking analysis methods and business knowledge - business indicators

Bing. Site Internet

Mavn builds nexus private server

LeetCode刷题——统计各位数字都不同的数字个数#357#Medium

LeetCode刷题——递增的三元子序列#334#Medium

Pytoch saves tensor to Mat file

Solve the problem of frequent interruption of mobaxterm remote connection

14_Redis_乐观锁
随机推荐
搭建自己的语义分割平台deeplabV3+
Leetcode skimming -- verifying the preorder serialization of binary tree # 331 # medium
2022 年辽宁省大学生数学建模A、B、C题(相关论文及模型程序代码网盘下载)
4. Data splitting of Flink real-time project
QML pop-up frame, customizable
【网络安全】网络资产收集
Application of CDN in game field
损失函数与正负样本分配:YOLO系列
Tidb data migration scenario overview
Principles, language, compilation, interpretation
Download blender on Alibaba cloud image station
How to intercept the value of a key from the JSON string returned by wechat?
Mavn builds nexus private server
19_Redis_宕机后手动配置主机
Leetcode skimming -- incremental ternary subsequence 334 medium
终于搞懂了JS中的事件循环,同步/异步,微任务/宏任务,运行机制(附笔试题)
XML Configuration File
06_ Stack and queue conversion
你不知道的Set集合
基于RZ/G2L | OK-G2LD-C开发板存储读写速度与网络实测