Skip to content

Commit

Permalink
2020-03-29
Browse files Browse the repository at this point in the history
  • Loading branch information
JiayangWu committed Mar 29, 2020
1 parent 0ea2d7b commit 260cc00
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions 1162.地图分析/1162-地图分析.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
class Solution(object):
def maxDistance(self, grid):
"""
:type grid: List[List[int]]
:rtype: int
"""
from collections import deque
m, n = len(grid), len(grid[0])
land = []
for i in range(m):
for j in range(n):
if grid[i][j] == 1:
land.append((i, j))

if not land or len(land) == m * n:
return -1

res = 0
dx = [1, -1, 0, 0]
dy = [0, 0, 1, -1]

queue = deque(land)
visited = set(land)
while queue:
for _ in range(len(queue)):
x0, y0 = queue.popleft()

for k in range(4):
x = x0 + dx[k]
y = y0 + dy[k]

if 0 <= x < m and 0 <= y < n and grid[x][y] == 0 and (x, y) not in visited:
queue.append((x, y))
visited.add((x, y))
res += 1
return res - 1


0 comments on commit 260cc00

Please sign in to comment.