Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from collections import deque
- class Solution(object):
- def neighbors(self, rooms, x, y):
- dx = [0,0,-1,1]
- dy = [-1,1,0,0]
- q = deque()
- q.append((x,y))
- step = 1
- while q:
- num = len(q)
- for i in range(0, num):
- xx, yy = q.popleft()
- for i in range(4):
- rx, ry = xx + dx[i], yy + dy[i]
- if rx < 0 or ry < 0 or rx > len(rooms)-1 or ry > len(rooms[0])-1:
- continue
- if rooms[rx][ry] > 0 and rooms[rx][ry] > step:
- rooms[rx][ry] = step
- q.append((rx, ry))
- step += 1
- def wallsAndGates(self, rooms):
- """
- :type rooms: List[List[int]]
- :rtype: void Do not return anything, modify rooms in-place instead.
- """
- if not rooms or not rooms[0]:
- return
- for i in range(0, len(rooms)):
- for j in range(0, len(rooms[0])):
- if rooms[i][j] == 0:
- self.neighbors(rooms, i, j)
Add Comment
Please, Sign In to add comment