-
Notifications
You must be signed in to change notification settings - Fork 319
/
Copy path289_GameOfLife.py
executable file
·43 lines (40 loc) · 1.44 KB
/
289_GameOfLife.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
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# @Author: [email protected]
class Solution(object):
def gameOfLife(self, board):
if not board:
return
m_rows = len(board)
n_cols = len(board[0])
count = [[0 for j in range(n_cols)] for i in range(m_rows)]
for i in range(m_rows):
for j in range(n_cols):
# Compute the number of live neighbors and
# Update the count of adjancent next cells meanwhile.
if j+1 < n_cols:
count[i][j] += board[i][j+1]
count[i][j+1] += board[i][j]
if i+1 < m_rows:
count[i][j] += board[i+1][j]
count[i+1][j] += board[i][j]
if j-1 >= 0:
count[i][j] += board[i+1][j-1]
count[i+1][j-1] += board[i][j]
if j+1 < n_cols:
count[i][j] += board[i+1][j+1]
count[i+1][j+1] += board[i][j]
# Current cell interacts with its eight neighbors
# Live cell turn dead
if board[i][j] and (count[i][j] < 2 or count[i][j] > 3):
board[i][j] = 0
# Dead cell turn live
if not board[i][j] and count[i][j] == 3:
board[i][j] = 1
return
"""
[[]]
[[0]]
[[1,1],[0,1]]
[[0,1,0],[1,1,1],[0,1,0]]
"""