Created
December 23, 2015 15:05
-
-
Save lyleaf/8b4fe811c683d3b030ed to your computer and use it in GitHub Desktop.
289. Game of Life
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution(object): | |
def gameOfLife(self, board): | |
""" | |
:type board: List[List[int]] | |
:rtype: void Do not return anything, modify board in-place instead. | |
""" | |
def Nnearby(i,j): | |
n = 0 | |
if j >= 1 and board[i][j-1]==1: n+=1 | |
if j+1 < len(board[0]) and board[i][j+1]==1: n+=1 | |
if j >= 1 and i >= 1 and board[i-1][j-1]==1: n+=1 | |
if i >= 1 and j+1 < len(board[0]) and board[i-1][j+1]==1: n+=1 | |
if i+1 < len(board) and j >= 1 and board[i+1][j-1]==1: n+=1 | |
if i+1 < len(board) and j+1 < len(board[0]) and board[i+1][j+1]==1: n+=1 | |
if i+1 < len(board) and board[i+1][j]==1: n+=1 | |
if i >= 1 and board[i-1][j]==1: n+=1 | |
return n | |
l = [] | |
for i in xrange(len(board)): | |
for j in xrange(len(board[0])): | |
if board[i][j] == 1: | |
if Nnearby(i,j) == 2 or Nnearby(i,j) == 3: | |
l.append([i,j]) | |
elif board[i][j] == 0: | |
if Nnearby(i,j) == 3: | |
l.append([i,j]) | |
for i in xrange(len(board)): | |
for j in xrange(len(board[0])): | |
board[i][j] = 0 | |
for x in l: | |
board[x[0]][x[1]] = 1 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
0,2 are "dead", and "dead->live" 1,3 are "live", and "live->dead" | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def gameOfLife(self, board): | |
m,n = len(board), len(board[0]) | |
for i in range(m): | |
for j in range(n): | |
if board[i][j] == 0 or board[i][j] == 2: | |
if self.nnb(board,i,j) == 3: | |
board[i][j] = 2 | |
else: | |
if self.nnb(board,i,j) < 2 or self.nnb(board,i,j) >3: | |
board[i][j] = 3 | |
for i in range(m): | |
for j in range(n): | |
if board[i][j] == 2: board[i][j] = 1 | |
if board[i][j] == 3: board[i][j] = 0 | |
def nnb(self, board, i, j): | |
m,n = len(board), len(board[0]) | |
count = 0 | |
if i-1 >= 0 and j-1 >= 0: count += board[i-1][j-1]%2 | |
if i-1 >= 0: count += board[i-1][j]%2 | |
if i-1 >= 0 and j+1 < n: count += board[i-1][j+1]%2 | |
if j-1 >= 0: count += board[i][j-1]%2 | |
if j+1 < n: count += board[i][j+1]%2 | |
if i+1 < m and j-1 >= 0: count += board[i+1][j-1]%2 | |
if i+1 < m: count += board[i+1][j]%2 | |
if i+1 < m and j+1 < n: count += board[i+1][j+1]%2 | |
return count |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment