-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWalls_and_Gates(Premium Problem).py
57 lines (47 loc) · 1.4 KB
/
Walls_and_Gates(Premium Problem).py
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
from typing import (
List,
)
from collections import deque
import math
class Node:
def __init__(self,x,y,time):
self.x=x
self.y=y
self.time=time
class Solution:
"""
@param rooms: m x n 2D grid
@return: nothing
"""
def walls_and_gates(self, grid: List[List[int]]):
# write your code here
empty=2147483647
row=len(grid)
cols=len(grid[0])
queue=deque()
for i in range(row):
for j in range(cols):
if grid[i][j] == 0:
node = Node(i,j,0)
queue.append(node)
while(queue):
curr_node = queue.popleft()
x=curr_node.x
y=curr_node.y
time=curr_node.time
if x-1>=0 and grid[x-1][y]==empty:
grid[x-1][y] = time+1
node=Node(x-1,y,time+1)
queue.append(node)
if x+1<row and grid[x+1][y]==empty:
grid[x+1][y] = time+1
node=Node(x+1,y,time+1)
queue.append(node)
if y-1>=0 and grid[x][y-1]==empty:
grid[x][y-1] = time+1
node=Node(x,y-1,time+1)
queue.append(node)
if y+1<cols and grid[x][y+1]==empty:
grid[x][y+1] = time+1
node=Node(x,y+1,time+1)
queue.append(node)