-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path542_01_matrix
More file actions
32 lines (27 loc) · 957 Bytes
/
542_01_matrix
File metadata and controls
32 lines (27 loc) · 957 Bytes
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
class Solution(object):
def updateMatrix(self, matrix):
"""
:type matrix: List[List[int]]
:rtype: List[List[int]]
"""
R, C = len(matrix), len(matrix[0])
rtn = matrix
def bfs(r,c):
toCheck = [[r, c]]
cnt = -1
while toCheck:
cnt += 1
temp = []
for m, n in toCheck:
if matrix[m][n] == 0:
rtn[r][c] = cnt
return
if m-1>=0 :temp.append([m-1,n])
if n-1>=0 :temp.append([m,n-1])
if m<R-1 :temp.append([m+1,n])
if n<C-1 :temp.append([m,n+1])
toCheck = temp
for r in range(R):
for c in range(C):
bfs(r, c)
return rtn