You are given an m x n grid rooms initialized with these three possible values.
-1 A wall or an obstacle.
0 A gate.
INF Infinity means an empty room. We use the value 231 - 1 = 2147483647 to represent INF as you may assume that the distance to a gate is less than 2147483647.
Fill each empty room with the distance to its nearest gate. If it is impossible to reach a gate, it should be filled with INF.
classSolution:defwallsAndGates(self,rooms:List[List[int]])->None:""" Do not return anything, modify rooms in-place instead. """m,n=len(rooms),len(rooms[0])inf=2**31-1q=deque([(i,j)foriinrange(m)forjinrange(n)ifrooms[i][j]==0])d=0whileq:d+=1for_inrange(len(q)):i,j=q.popleft()fora,bin[[0,1],[0,-1],[1,0],[-1,0]]:x,y=i+a,j+bif0<=x<mand0<=y<nandrooms[x][y]==inf:rooms[x][y]=dq.append((x,y))