当前位置:网站首页>【LeetCode】1162-地图分析
【LeetCode】1162-地图分析
2022-07-02 12:09:00 【酥酥~】
你现在手里有一份大小为 n x n 的 网格 grid,上面的每个 单元格 都用 0 和 1 标记好了。其中 0 代表海洋,1 代表陆地。
请你找出一个海洋单元格,这个海洋单元格到离它最近的陆地单元格的距离是最大的,并返回该距离。如果网格上只有陆地或者海洋,请返回 -1。
我们这里说的距离是「曼哈顿距离」( Manhattan Distance):(x0, y0) 和 (x1, y1) 这两个单元格之间的距离是 |x0 - x1| + |y0 - y1| 。
示例 1:

输入: grid = [[1,0,1],[0,0,0],[1,0,1]]
输出: 2
解释:
海洋单元格 (1, 1) 和所有陆地单元格之间的距离都达到最大,最大距离为 2。
示例 2:

输入: grid = [[1,0,0],[0,0,0],[0,0,0]]
输出: 4
解释:
海洋单元格 (2, 2) 和所有陆地单元格之间的距离都达到最大,最大距离为 4。
提示:
- n == grid.length
- n == grid[i].length
- 1 <= n <= 100
- grid[i][j] 不是 0 就是 1
思路:
广度优先遍历,从每一块陆地开始,给所有的能到达的海洋赋值(距离)
#广度优先遍历
class Solution(object):
def maxDistance(self,grid):
n = len(grid)#获取数列大小
for i in range(n):
for j in range(n):
if grid[i][j] == 1:#遇见陆地,开始广度遍历给海洋赋值(即距离)
queue = []
queue.append((i,j))
while queue:
x,y = queue[0]
tmp = grid[x][y]+1
queue.pop(0)
for xx,yy in [(x,y+1),(x,y-1),(x+1,y),(x-1,y)]:
if 0<=xx<n and 0<=yy<n and (grid[xx][yy]==0 or grid[xx][yy]>tmp):
grid[xx][yy] = tmp
queue.append((xx,yy))
result = 0
for k in grid:
result = max(result,max(k))
return result-1 if result!=1 and result!=0 else -1
边栏推荐
- There are 7 seats with great variety, Wuling Jiachen has outstanding product power, large humanized space, and the key price is really fragrant
- 党史纪实主题公益数字文创产品正式上线
- 夏季高考文化成绩一分一段表
- 05_队列
- 怎样从微信返回的json字符串中截取某个key的值?
- 数据分析思维分析方法和业务知识——业务指标
- MySQL calculate n-day retention rate
- Pytorch 保存tensor到.mat文件
- List集合&UML图
- 04.进入云原生后的企业级应用构建的一些思考
猜你喜欢

14_ Redis_ Optimistic lock

FPGA - 7系列 FPGA内部结构之Clocking -03- 时钟管理模块(CMT)

15_Redis_Redis.conf详解

飞凌嵌入式RZ/G2L处理器核心板及开发板上手评测

MySQL calculate n-day retention rate

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

07_ Hash

Storage read-write speed and network measurement based on rz/g2l | ok-g2ld-c development board

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

彻底弄懂浏览器强缓存和协商缓存
随机推荐
语义分割学习笔记(一)
There are 7 seats with great variety, Wuling Jiachen has outstanding product power, large humanized space, and the key price is really fragrant
Engineer evaluation | rk3568 development board hands-on test
03.golang初步使用
Leetcode skimming -- verifying the preorder serialization of binary tree # 331 # medium
Bing. Com website
Leetcode skimming - remove duplicate letters 316 medium
搭建自己的语义分割平台deeplabV3+
高考录取分数线爬虫
Tidb data migration scenario overview
05_ queue
[solution] educational codeforces round 82
Recommended configuration of tidb software and hardware environment
Semantic segmentation learning notes (1)
XML配置文件
Force deduction solution summarizes the lucky numbers in 1380 matrix
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
Party History Documentary theme public welfare digital cultural and creative products officially launched
PHP method to get the index value of the array item with the largest key value in the array
vChain: Enabling Verifiable Boolean Range Queries over Blockchain Databases(sigmod‘2019)