-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem286.py
More file actions
80 lines (62 loc) · 1.77 KB
/
problem286.py
File metadata and controls
80 lines (62 loc) · 1.77 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
'''
286. Walls and Gates
Medium
You are given a m x n 2D grid initialized with these three possible values.
-1 - A wall or an obstacle.
0 - A gate.
INF - Infinity means an empty room. We use the value 231 - 1 = 2147483647 to represent INF as you may assume that the distance to a gate is less than 2147483647.
Fill each empty room with the distance to its nearest gate. If it is impossible to reach a gate, it should be filled with INF.
Example:
Given the 2D grid:
INF -1 0 INF
INF INF INF -1
INF -1 INF -1
0 -1 INF INF
After running your function, the 2D grid should be:
3 -1 0 1
2 2 1 -1
1 -1 2 -1
0 -1 3 4
Solution:
Simple BFS problem, only remember to check if the neightbor can be update to a smaller value, if so, update the value and
add the neightbor to the next round of BFS, since we need to update everything.
Again, simple bfs.
'''
class Solution(object):
def wallsAndGates(self, rooms):
if not rooms:
return
if not rooms[0]:
return
M = len(rooms)
N = len(rooms[0])
x = [0,0,1,-1]
y = [1,-1,0,0]
def bfs(node):
currentValue = rooms[node[0]][node[1]]
stack = [node]
while stack:
tmp = []
while stack:
currentNode = stack.pop()
for i in range(4):
nx = x[i] + currentNode[0]
ny = y[i] + currentNode[1]
if 0 <= nx < M and 0 <= ny < N:
if rooms[nx][ny] > rooms[currentNode[0]][currentNode[1]] + 1:
rooms[nx][ny] = rooms[currentNode[0]][currentNode[1]] + 1
tmp.append((nx,ny))
stack = tmp
for i in range(M):
for k in range(N):
if rooms[i][k] == 0:
bfs((i,k))
inf = float('inf')
rooms = [ [inf, -1, 0, inf],
[inf, inf, inf , -1],
[inf, -1, inf, -1],
[0, -1, inf, inf]]
S = Solution()
S.wallsAndGates(rooms)
for i in rooms:
print i