forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame of Life.py
37 lines (35 loc) · 908 Bytes
/
Game of Life.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
#pattern
actual update ref
0 0 0
1 1 1
0 1 -1
1 0 -2
class Solution:
def gameOfLife(self, board: List[List[int]]) -> None:
r = len(board)
c = len(board[0])
ans = [[0]*c for _ in range(r)]
neighs = [[1,0],[-1,0],[0,1],[0,-1],[-1,-1],[-1,1],[1,1],[1,-1]]
for i in range(r):
for j in range(c):
livecnt,deadcnt = 0,0
for di,dj in neighs:
if 0<=(i+di) < r and 0<=(j+dj)<c:
if board[i+di][j+dj] == 0 or board[i+di][j+dj] == -1 :
deadcnt+=1
else:
livecnt+=1
if board[i][j] == 0:
if livecnt == 3:
board[i][j] = -1
else:
if livecnt == 2 or livecnt==3:
board[i][j] = 1
else:
board[i][j] = -2
for i in range(r):
for j in range(c):
if board[i][j] == -1:
board[i][j] = 1
elif board[i][j] == -2:
board[i][j] = 0