当前位置:网站首页>【LeetCode】1020-飞地的数量
【LeetCode】1020-飞地的数量
2022-07-02 12:09:00 【酥酥~】
给你一个大小为 m x n 的二进制矩阵 grid ,其中 0 表示一个海洋单元格、1 表示一个陆地单元格。
一次 移动 是指从一个陆地单元格走到另一个相邻(上、下、左、右)的陆地单元格或跨过 grid 的边界。
返回网格中 无法 在任意次数的移动中离开网格边界的陆地单元格的数量。
示例 1:
输入: grid = [[0,0,0,0],[1,0,1,0],[0,1,1,0],[0,0,0,0]]
输出: 3
解释: 有三个 1 被 0 包围。一个 1 没有被包围,因为它在边界上。
示例 2:
输入: grid = [[0,1,1,0],[0,0,1,0],[0,0,1,0],[0,0,0,0]]
输出: 0
解释: 所有 1 都在边界上或可以到达边界。
提示:
- m == grid.length
- n == grid[i].length
- 1 <= m, n <= 500
- grid[i][j] 的值为 0 或 1
#广度优先遍历
class Solution(object):
def numEnclaves(self, grid):
m = len(grid)
n = len(grid[0])
result = 0
for i in range(m):
for j in range(n):
if grid[i][j]==1:
queue = []
queue.append((i,j))
grid[i][j]=0
cnt = 1#统计每一块岛屿的陆地数量
flag = 1#标志岛屿是否为有在边界的陆地
while queue:
x,y = queue[0]
queue.pop(0)
for xx,yy in [(x+1,y),(x-1,y),(x,y-1),(x,y+1)]:
if not 0<=xx<m or not 0<=yy<n:
flag = 0#如果有陆地在边界,则这块岛屿的所有陆地都是是飞地
elif grid[xx][yy]==1:
queue.append((xx,yy))
grid[xx][yy]=0
cnt+=1
if flag:
result+=cnt
return result
#深度优先遍历
class Solution(object):
def numEnclaves(self, grid):
m = len(grid)
n = len(grid[0])
result = 0
for i in range(m):
for j in range(n):
if grid[i][j]==1:
stack = []
stack.append((i,j))
grid[i][j]=0
cnt = 1
flag = 1
while stack:
x,y = stack[-1]
stack.pop()
for xx,yy in [(x+1,y),(x-1,y),(x,y-1),(x,y+1)]:
if not 0<=xx<m or not 0<=yy<n:
flag = 0
elif grid[xx][yy]==1:
stack.append((xx,yy))
grid[xx][yy]=0
cnt+=1
if flag:
result+=cnt
return result
边栏推荐
- folium,确诊和密接轨迹上图
- 02_ Linear table_ Sequence table
- 03.golang初步使用
- SQL transaction
- MySQL calculate n-day retention rate
- 终于搞懂了JS中的事件循环,同步/异步,微任务/宏任务,运行机制(附笔试题)
- 12_ Redis_ Bitmap_ command
- Practice of compiling principle course -- implementing an interpreter or compiler of elementary function operation language
- MD5加密
- 5. Practice: jctree implements the annotation processor at compile time
猜你喜欢

vChain: Enabling Verifiable Boolean Range Queries over Blockchain Databases(sigmod‘2019)

05_ queue

07_ Hash

Steps for Navicat to create a new database

04_ 栈

Equipped with Ti am62x processor, Feiling fet6254-c core board is launched!

07_哈希

Pytorch 保存tensor到.mat文件

03_線性錶_鏈錶

15_ Redis_ Redis. Conf detailed explanation
随机推荐
LeetCode刷题——递增的三元子序列#334#Medium
04_ 栈
Kibana basic operation
百变大7座,五菱佳辰产品力出众,人性化大空间,关键价格真香
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
Engineer evaluation | rk3568 development board hands-on test
2022 college students in Liaoning Province mathematical modeling a, B, C questions (related papers and model program code online disk download)
06_栈和队列转换
Download blender on Alibaba cloud image station
Tidb data migration scenario overview
How to choose a third-party software testing organization for automated acceptance testing of mobile applications
Libcurl Lesson 13 static library introduces OpenSSL compilation dependency
4. Jctree related knowledge learning
17_Redis_Redis发布订阅
MD5 encryption
高考分数线爬取
How to conduct TPC-C test on tidb
21_ Redis_ Analysis of redis cache penetration and avalanche
15_ Redis_ Redis. Conf detailed explanation
20_ Redis_ Sentinel mode