当前位置:网站首页>【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
边栏推荐
- 搭载TI AM62x处理器,飞凌FET6254-C核心板首发上市!
- Loss function and positive and negative sample allocation: Yolo series
- Oracle primary key auto increment
- Huffman tree: (1) input each character and its weight (2) construct Huffman tree (3) carry out Huffman coding (4) find hc[i], and get the Huffman coding of each character
- YOLOV5 代码复现以及搭载服务器运行
- 13_Redis_事务
- LeetCode刷题——验证二叉树的前序序列化#331#Medium
- Leetcode question brushing - parity linked list 328 medium
- The traversal methods of binary tree mainly include: first order traversal, middle order traversal, second order traversal, and hierarchical traversal. First order, middle order, and second order actu
- Infra11199 database system
猜你喜欢
03_ Linear table_ Linked list
飞凌嵌入式RZ/G2L处理器核心板及开发板上手评测
基于RZ/G2L | OK-G2LD-C开发板存储读写速度与网络实测
Data analysis thinking analysis methods and business knowledge - business indicators
21_ Redis_ Analysis of redis cache penetration and avalanche
07_ Hash
Build your own semantic segmentation platform deeplabv3+
Bing.com网站
终于搞懂了JS中的事件循环,同步/异步,微任务/宏任务,运行机制(附笔试题)
Solution of Queen n problem
随机推荐
Steps for Navicat to create a new database
13_Redis_事务
04_ Stack
Solution of Queen n problem
11_ Redis_ Hyperloglog_ command
02. After containerization, you must face golang
[solution] educational codeforces round 82
Tidb cross data center deployment topology
让您的HMI更具优势,FET-G2LD-C核心板是个好选择
07_ Hash
03. Preliminary use of golang
15_Redis_Redis.conf详解
搭建自己的语义分割平台deeplabV3+
The traversal methods of binary tree mainly include: first order traversal, middle order traversal, second order traversal, and hierarchical traversal. First order, middle order, and second order actu
Learn the method code example of converting timestamp to uppercase date using PHP
folium,确诊和密接轨迹上图
vChain: Enabling Verifiable Boolean Range Queries over Blockchain Databases(sigmod‘2019)
03_ Linear table_ Linked list
Record an interview
Tidb environment and system configuration check