286墙与门
class Solution:
def wallsAndGates(self, rooms):
EMPTYROOM = 2**31 - 1
door = 0
neighbors = [[-1,0], [1,0], [0,-1], [0,1]]
if not rooms:
return
row = len(rooms)
col = len(rooms[0])
searchlist = deque()
for i in range(row):
for j in range(col):
if rooms[i][j] == door:
searchlist.append(([i,j], 0))
while searchlist:
[n_row, n_col], distance = searchlist.popleft()
for d_row, d_col in neighbors:
neighbor_row = d_row + n_row
neighbor_col = d_col + n_col
if 0 <= neighbor_row < row and 0<=neighbor_col < col \
and rooms[neighbor_row][neighbor_col] == EMPTYROOM:
rooms[neighbor_row][neighbor_col] = distance + 1
searchlist.append(([neighbor_row,neighbor_col], distance + 1))
return print(rooms)
data = [[2147483647,-1,0,2147483647],[2147483647,2147483647,2147483647,-1],[2147483647,-1,2147483647,-1],[0,-1,2147483647,2147483647]]
data1 = []
data2 = [[-1]]
data3 = [[2147483647]]
data4 = [[2147483647,0,2147483647,2147483647,0,2147483647,-1,2147483647]]
solution = Solution()
solution.wallsAndGates(data)