当前位置:网站首页>【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
边栏推荐
- 05_ queue
- Infra11199 database system
- 夏季高考文化成绩一分一段表
- 14_Redis_乐观锁
- 让您的HMI更具优势,FET-G2LD-C核心板是个好选择
- Force deduction solution summary 2029 stone game IX
- 03. Preliminary use of golang
- Evaluation of embedded rz/g2l processor core board and development board of Feiling
- FPGA - 7系列 FPGA内部结构之Clocking -03- 时钟管理模块(CMT)
- 飞凌嵌入式RZ/G2L处理器核心板及开发板上手评测
猜你喜欢

08_ strand

21_Redis_浅析Redis缓存穿透和雪崩

Markdown tutorial

There are 7 seats with great variety, Wuling Jiachen has outstanding product power, large humanized space, and the key price is really fragrant

Leetcode skimming -- verifying the preorder serialization of binary tree # 331 # medium

How to avoid 7 common problems in mobile and network availability testing

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

02_线性表_顺序表

怎样从微信返回的json字符串中截取某个key的值?

自定义异常
随机推荐
Deploy tidb cluster with tiup
百变大7座,五菱佳辰产品力出众,人性化大空间,关键价格真香
13_Redis_事务
Evaluation of embedded rz/g2l processor core board and development board of Feiling
Equipped with Ti am62x processor, Feiling fet6254-c core board is launched!
How to solve the problem of database content output
How to choose a third-party software testing organization for automated acceptance testing of mobile applications
SQL transaction
MD5加密
Let your HMI have more advantages. Fet-g2ld-c core board is a good choice
Libcurl Lesson 13 static library introduces OpenSSL compilation dependency
2022 年辽宁省大学生数学建模A、B、C题(相关论文及模型程序代码网盘下载)
Leetcode skimming -- incremental ternary subsequence 334 medium
Infra11199 database system
党史纪实主题公益数字文创产品正式上线
03_ Linear table_ Linked list
10_ Redis_ geospatial_ command
18_ Redis_ Redis master-slave replication & cluster building
Tidb environment and system configuration check
03. Preliminary use of golang