当前位置:网站首页>[leetcode] 1162 map analysis
[leetcode] 1162 map analysis
2022-07-02 15:36:00 【Crisp ~】
Now you have a copy of n x n Of grid grid, Each of the above Cell Use both 0 and 1 It's marked . among 0 On behalf of the ocean ,1 Representing land .
Please find an ocean cell , The ocean cell has the largest distance to the nearest land cell , And return the distance . If there's only land or ocean on the grid , Please return -1.
The distance we're talking about here is 「 Manhattan distance 」( Manhattan Distance):(x0, y0) and (x1, y1) The distance between these two cells is |x0 - x1| + |y0 - y1| .
Example 1:
Input : grid = [[1,0,1],[0,0,0],[1,0,1]]
Output : 2
explain :
Ocean cell (1, 1) And the distance between all the land cells reaches the maximum , The maximum distance is 2.
Example 2:
Input : grid = [[1,0,0],[0,0,0],[0,0,0]]
Output : 4
explain :
Ocean cell (2, 2) And the distance between all the land cells reaches the maximum , The maximum distance is 4.
Tips :
- n == grid.length
- n == grid[i].length
- 1 <= n <= 100
- grid[i][j] No 0 Namely 1
Ideas :
Breadth first traversal , Start with every piece of land , Assign values to all accessible oceans ( distance )
# Breadth first traversal
class Solution(object):
def maxDistance(self,grid):
n = len(grid)# Get the size of the sequence
for i in range(n):
for j in range(n):
if grid[i][j] == 1:# Meet land , Start breadth traversal and assign values to the ocean ( Distance )
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
边栏推荐
- Basic knowledge of cryptography
- 4. Data splitting of Flink real-time project
- Leetcode skimming -- verifying the preorder serialization of binary tree # 331 # medium
- Summary of the first three passes of sqli Labs
- 语义分割学习笔记(一)
- 【网络安全】网络资产收集
- Map introduction
- 05_ queue
- Storage read-write speed and network measurement based on rz/g2l | ok-g2ld-c development board
- Data analysis thinking analysis methods and business knowledge - business indicators
猜你喜欢
Oracle primary key auto increment
Bing.com網站
Data analysis thinking analysis methods and business knowledge - business indicators
Yolov5 code reproduction and server operation
. Solution to the problem of Chinese garbled code when net core reads files
搭建自己的语义分割平台deeplabV3+
损失函数与正负样本分配:YOLO系列
YOLOV5 代码复现以及搭载服务器运行
07_ Hash
06_ Stack and queue conversion
随机推荐
05_ queue
13_ Redis_ affair
02.面向容器化后,必须面对golang
党史纪实主题公益数字文创产品正式上线
【LeetCode】695-岛屿的最大面积
MD5 encryption
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
Map introduction
06_ Stack and queue conversion
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
(Video + graphic) machine learning introduction series - Chapter 5 machine learning practice
怎样从微信返回的json字符串中截取某个key的值?
Pytorch 保存tensor到.mat文件
终于搞懂了JS中的事件循环,同步/异步,微任务/宏任务,运行机制(附笔试题)
MySQL -- Index Optimization -- order by
Solution of Queen n problem
19_ Redis_ Manually configure the host after downtime
07_ Hash
高考分数线爬取
自定义异常