Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution(object):
- def wallsAndGates(self, rooms):
- """
- :type rooms: List[List[int]]
- :rtype: None Do not return anything, modify rooms in-place instead.
- """
- from collections import deque
- INF = 2147483647
- q = deque()
- n, m = len(rooms), len(rooms[0])
- for i in range(n):
- for j in range(m):
- if rooms[i][j] == 0:
- q.append((i, j))
- count = 1
- while q:
- row, col = q.popleft()
- directions = [
- (row+1, col),
- (row-1, col),
- (row, col+1),
- (row, col-1)
- ]
- for (r, c) in directions:
- if r in range(n) and c in range(m) and rooms[r][c] == INF:
- q.append((r, c))
- rooms[r][c] = rooms[row][col] + 1
- count += 1
Advertisement
Add Comment
Please, Sign In to add comment