-
Notifications
You must be signed in to change notification settings - Fork 0
/
598_Zombie_in_Matrix.py
63 lines (50 loc) · 1.6 KB
/
598_Zombie_in_Matrix.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
58
59
60
61
62
63
class Solution:
"""
@param grid: a 2D integer grid
@return: an integer
"""
PEOPLE = 0
ZOMBIE = 1
WALL = 2
def zombie(self, grid):
dirs = [(1, 0), (-1, 0), (0, 1), (0, -1)]
if grid is None or len(grid) == 0 or len(grid[0]) == 0:
return 0
n, m = len(grid), len(grid[0])
print(grid)
cpGrid = []
for col in grid:
cpGrid.append(col)
print(cpGrid)
q = []
people_left = 0
days = 0
for i in range(n):
for j in range(m):
if cpGrid[i][j] == self.PEOPLE:
people_left += 1
elif cpGrid[i][j] == self.ZOMBIE:
q.append((i, j))
if people_left == 0:
return 0
# BFS
while len(q) > 0:
days += 1
size = len(q)
for i in range(size):
p = q.pop(0)
for dx, dy in dirs:
bitex = p[0] + dx
bitey = p[1] + dy
# check if out of boundary
if bitex < 0 or bitex >= n or bitey < 0 or bitey >= m:
continue
# check if bitetable
if cpGrid[bitex][bitey] is not self.PEOPLE:
continue
people_left -= 1
cpGrid[bitex][bitey] = self.ZOMBIE
q.append((bitex, bitey))
if people_left == 0:
return days
return -1