当前位置:网站首页>[leetcode] 1254 - count the number of closed Islands
[leetcode] 1254 - count the number of closed Islands
2022-07-02 15:36:00 【Crisp ~】
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 from 1 Surround ( Left 、 On 、 Right 、 Next ) The island of .
Please return Close the island Number of .
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 : 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]]
Output : 2
Tips :
- 1 <= grid.length, grid[0].length <= 100
- 0 <= grid[i][j] <=1
# Breadth first traversal
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
# Depth-first traversal
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
边栏推荐
猜你喜欢
Party History Documentary theme public welfare digital cultural and creative products officially launched
19_ Redis_ Manually configure the host after downtime
Map introduction
SQL stored procedure
Be a good gatekeeper on the road of anti epidemic -- infrared thermal imaging temperature detection system based on rk3568
Case introduction and problem analysis of microservice
Semantic segmentation learning notes (1)
SQL transaction
03_線性錶_鏈錶
17_Redis_Redis发布订阅
随机推荐
【LeetCode】417-太平洋大西洋水流问题
How to intercept the value of a key from the JSON string returned by wechat?
02_ Linear table_ Sequence table
JVM architecture, classloader, parental delegation mechanism
Custom exception
18_Redis_Redis主从复制&&集群搭建
. Net again! Happy 20th birthday
YOLOV5 代码复现以及搭载服务器运行
2022 college students in Liaoning Province mathematical modeling a, B, C questions (related papers and model program code online disk download)
Download blender on Alibaba cloud image station
11_ Redis_ Hyperloglog_ command
14_ Redis_ Optimistic lock
How does the computer set up speakers to play microphone sound
士官类学校名录
Be a good gatekeeper on the road of anti epidemic -- infrared thermal imaging temperature detection system based on rk3568
folium地图无法显示的问题,临时性解决方案如下
Yolov5 code reproduction and server operation
Engineer evaluation | rk3568 development board hands-on test
Beijing rental data analysis
损失函数与正负样本分配:YOLO系列