-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsol.py
44 lines (37 loc) · 1.21 KB
/
sol.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
from typing import List
class Solution:
def wallsAndGates(self, rooms: List[List[int]]) -> None:
spots = []
# find all the gates
for row in range(len(rooms)):
for col in range(len(rooms[0])):
cur = rooms[row][col]
if cur == 0:
spots.append((row, col, 0))
visited = {}
while spots:
[row, col, dist] = spots.pop(0)
if row < 0 or row >= len(rooms):
continue
if col < 0 or col >= len(rooms[0]):
continue
key = f"{row}_{col}"
if key in visited:
continue
visited[key] = True
cur = rooms[row][col]
if cur == -1:
continue
rooms[row][col] = dist
spots.append((row + 1, col, dist + 1))
spots.append((row - 1, col, dist + 1))
spots.append((row, col + 1, dist + 1))
spots.append((row, col - 1, dist + 1))
arr = [
[2147483647,-1,0,2147483647],
[2147483647,2147483647,2147483647,-1],
[2147483647,-1,2147483647,-1],
[0,-1,2147483647,2147483647]]
s = Solution()
s.wallsAndGates(arr)
print(arr)