当前位置:网站首页>【LeetCode】1905-统计子岛屿
【LeetCode】1905-统计子岛屿
2022-07-02 12:09:00 【酥酥~】
给你两个 m x n 的二进制矩阵 grid1 和 grid2 ,它们只包含 0 (表示水域)和 1 (表示陆地)。一个 岛屿 是由 四个方向 (水平或者竖直)上相邻的 1 组成的区域。任何矩阵以外的区域都视为水域。
如果 grid2 的一个岛屿,被 grid1 的一个岛屿 完全 包含,也就是说 grid2 中该岛屿的每一个格子都被 grid1 中同一个岛屿完全包含,那么我们称 grid2 中的这个岛屿为 子岛屿 。
请你返回 grid2 中 子岛屿 的 数目 。
示例 1:
输入: grid1 = [[1,1,1,0,0],[0,1,1,1,1],[0,0,0,0,0],[1,0,0,0,0],[1,1,0,1,1]], grid2 = [[1,1,1,0,0],[0,0,1,1,1],[0,1,0,0,0],[1,0,1,1,0],[0,1,0,1,0]]
输出: 3
解释: 如上图所示,左边为 grid1 ,右边为 grid2 。
grid2 中标红的 1 区域是子岛屿,总共有 3 个子岛屿。
示例 2:
**输入:**grid1 = [[1,0,1,0,1],[1,1,1,1,1],[0,0,0,0,0],[1,1,1,1,1],[1,0,1,0,1]], grid2 = [[0,0,0,0,0],[1,1,1,1,1],[0,1,0,1,0],[0,1,0,1,0],[1,0,0,0,1]]
**输出:**2
**解释:**如上图所示,左边为 grid1 ,右边为 grid2 。
grid2 中标红的 1 区域是子岛屿,总共有 2 个子岛屿。
提示:
- m == grid1.length == grid2.length
- n == grid1[i].length == grid2[i].length
- 1 <= m, n <= 500
- grid1[i][j] 和 grid2[i][j] 都要么是 0 要么是 1 。
#广度优先遍历
class Solution(object):
def countSubIslands(self, grid1, grid2):
m = len(grid2)
n = len(grid2[0])
result = 0#统计子岛屿数量
for i in range(m):
for j in range(n):
if grid2[i][j] == 1:
queue = []
grid2[i][j] = 0
queue.append((i,j))
flag = 1#标志是否为子岛屿
while queue:
x,y = queue[0]
queue.pop(0)
if grid1[x][y] == 0:#如果在grid1中对应的位置为海洋,则该岛屿不是子岛屿
flag = 0
for xx,yy in [(x+1,y),(x-1,y),(x,y+1),(x,y-1)]:
if 0<=xx<m and 0<=yy<n and grid2[xx][yy]==1:
grid2[xx][yy] = 0
queue.append((xx,yy))
result += flag
return result
#深度优先遍历
class Solution(object):
def countSubIslands(self, grid1, grid2):
m = len(grid2)
n = len(grid2[0])
result = 0
for i in range(m):
for j in range(n):
if grid2[i][j] == 1:
stack = []
grid2[i][j] = 0
stack.append((i,j))
flag = 1
while stack:
x,y = stack[-1]
stack.pop()
if grid1[x][y] == 0:
flag = 0
for xx,yy in [(x+1,y),(x-1,y),(x,y+1),(x,y-1)]:#四个方向遍历
if 0<=xx<m and 0<=yy<n and grid2[xx][yy]==1:
grid2[xx][yy] = 0
stack.append((xx,yy))
result += flag
return result
边栏推荐
- Topology architecture of the minimum deployment of tidb cluster
- Bing.com網站
- How does the computer set up speakers to play microphone sound
- Mavn builds nexus private server
- Leetcode skimming - remove duplicate letters 316 medium
- folium地图无法显示的问题,临时性解决方案如下
- 11_Redis_Hyperloglog_命令
- 【Leetcode】167-两数之和II -输入有序数组
- MySQL calculate n-day retention rate
- 13_ Redis_ affair
猜你喜欢

14_Redis_乐观锁

4. Jctree related knowledge learning

基于RZ/G2L | OK-G2LD-C开发板存储读写速度与网络实测

Guangzhou Emergency Management Bureau issued a high temperature and high humidity chemical safety reminder in July

【LeetCode】417-太平洋大西洋水流问题

03_线性表_链表

03.golang初步使用

PTA 天梯赛习题集 L2-001 城市间紧急救援

SQL transaction

Solve the problem of frequent interruption of mobaxterm remote connection
随机推荐
19_Redis_宕机后手动配置主机
党史纪实主题公益数字文创产品正式上线
Practice of compiling principle course -- implementing an interpreter or compiler of elementary function operation language
Download blender on Alibaba cloud image station
06_ Stack and queue conversion
I made an istio workshop. This is the first introduction
03_線性錶_鏈錶
让您的HMI更具优势,FET-G2LD-C核心板是个好选择
Application of CDN in game field
04.进入云原生后的企业级应用构建的一些思考
16_ Redis_ Redis persistence
损失函数与正负样本分配:YOLO系列
【Leetcode】167-两数之和II -输入有序数组
Build your own semantic segmentation platform deeplabv3+
Leetcode skimming -- incremental ternary subsequence 334 medium
Tidb data migration scenario overview
6.12 critical moment of Unified Process Platform
Force deduction solution summarizes the lucky numbers in 1380 matrix
JVM architecture, classloader, parental delegation mechanism
14_ Redis_ Optimistic lock