-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathm542 multi sorce bfs.py
31 lines (26 loc) · 1.04 KB
/
m542 multi sorce bfs.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
class Solution:
def updateMatrix(self, mat: List[List[int]]) -> List[List[int]]:
visited = set()
toVisit = deque()
for row in range(len(mat)) :
for col in range(len(mat[0])) :
if mat[row][col] == 0 :
visited.add((row, col))
toVisit.append((row + 1, col, 1))
toVisit.append((row - 1, col, 1))
toVisit.append((row, col + 1, 1))
toVisit.append((row, col - 1, 1))
while toVisit :
row, col, dist = toVisit.popleft()
if not (0 <= row < len(mat)) or not (0 <= col < len(mat[0])) :
continue
if (row, col) in visited :
continue
visited.add((row, col))
mat[row][col] = dist
dist += 1
toVisit.append((row + 1, col, dist))
toVisit.append((row - 1, col, dist))
toVisit.append((row, col + 1, dist))
toVisit.append((row, col - 1, dist))
return mat