当前位置:网站首页>[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
边栏推荐
- [solution] educational codeforces round 82
- Leetcode skimming -- sum of two integers 371 medium
- 19_ Redis_ Manually configure the host after downtime
- 12_ Redis_ Bitmap_ command
- 21_ Redis_ Analysis of redis cache penetration and avalanche
- Bing.com網站
- 18_ Redis_ Redis master-slave replication & cluster building
- 18_Redis_Redis主从复制&&集群搭建
- SQL transaction
- Leetcode skimming -- count the number of numbers with different numbers 357 medium
猜你喜欢

Bing. Site Internet

15_ Redis_ Redis. Conf detailed explanation

Pytoch saves tensor to Mat file

Beijing rental data analysis

Guangzhou Emergency Management Bureau issued a high temperature and high humidity chemical safety reminder in July

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

终于搞懂了JS中的事件循环,同步/异步,微任务/宏任务,运行机制(附笔试题)

. Solution to the problem of Chinese garbled code when net core reads files

Download blender on Alibaba cloud image station

14_ Redis_ Optimistic lock
随机推荐
Custom exception
Solve the problem of frequent interruption of mobaxterm remote connection
Infra11199 database system
Force deduction solution summarizes the lucky numbers in 1380 matrix
QML pop-up frame, customizable
LeetCode刷题——奇偶链表#328#Medium
[leetcode] 189 rotation array
Facing the challenge of "lack of core", how can Feiling provide a stable and strong guarantee for customers' production capacity?
. Solution to the problem of Chinese garbled code when net core reads files
17_Redis_Redis发布订阅
百变大7座,五菱佳辰产品力出众,人性化大空间,关键价格真香
SQL stored procedure
LeetCode刷题——统计各位数字都不同的数字个数#357#Medium
[network security] network asset collection
Case introduction and problem analysis of microservice
微信支付宝账户体系和支付接口业务流程
03. Preliminary use of golang
Yolov5 code reproduction and server operation
【LeetCode】283-移动零
vChain: Enabling Verifiable Boolean Range Queries over Blockchain Databases(sigmod‘2019)