-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.py
56 lines (48 loc) · 1.46 KB
/
Solution.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
"""
input:
[[0,1,0,0,1],
[0,0,0,0,0],
[0,1,0,1,0],
[0,0,0,0,0]]
"""
def shortestDistance(grid):
if not grid or not grid[0]:
return -1
m, n = len(grid), len(grid[0])
dirs = [[-1,0],[1,0],[0,-1],[0,1]]
distance = [[0 for _ in range(n)] for _ in range(m)]
for i in range(m):
for j in range(n):
if grid[i][j] == 1:
visited = [[False for _ in range(n)] for _ in range(m)]
level = 1
q = [[i,j]]
while q:
N = len(q)
for _ in range(N):
x, y = q.pop(0)
for a, b in dirs:
nx, ny = x + a, y + b
# within range && not visited
if nx >= 0 and nx < m and ny >= 0 and ny < n and not visited[nx][ny] and grid[nx][ny] == 0:
# marked as visited
visited[nx][ny] = True
# construct distance field
distance[nx][ny] += level
# add neighbors to the queue
q.append([nx, ny])
level += 1
print("distance:{}".format(distance))
grid = [
[0,1,0,0,1],
[0,0,0,0,0],
[0,1,0,1,0],
[0,0,0,0,0]]
"""
distance:
[[15, 0, 9, 9, 0],
[13, 9, 9, 9, 11],
[15, 0, 9, 0, 13],
[17, 15, 13, 13, 15]]
"""
shortestDistance(grid)