# Game of Life 【leetcode】【98.9】【M】

223人阅读 评论(0)

According to the Wikipedia's article: "The Game of Life, also known simply as Life, is a cellular automaton devised by the British mathematician John Horton Conway in 1970."

Given a board with m by n cells, each cell has an initial state live (1) or dead (0). Each cell interacts with its eight neighbors (horizontal, vertical, diagonal) using the following four rules (taken from the above Wikipedia article):

1. Any live cell with fewer than two live neighbors dies, as if caused by under-population.
2. Any live cell with two or three live neighbors lives on to the next generation.
3. Any live cell with more than three live neighbors dies, as if by over-population..
4. Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.

Write a function to compute the next state (after one update) of the board given its current state.

1. Could you solve it in-place? Remember that the board needs to be updated at the same time: You cannot update some cells first and then use their updated values to update other cells.
2. In this question, we represent the board using a 2D array. In principle, the board is infinite, which would cause problems when the active area encroaches the border of the array. How would you address these problems?

1. 当前细胞为存活状态时，当周围低于2个（不包含2个）存活细胞时， 该细胞变成死亡状态。（模拟生命数量稀少）
2. 当前细胞为存活状态时，当周围有2个或3个存活细胞时， 该细胞保持原样。
3. 当前细胞为存活状态时，当周围有3个以上的存活细胞时，该细胞变成死亡状态。（模拟生命数量过多）
4. 当前细胞为死亡状态时，当周围有3个存活细胞时，该细胞变成存活状态。 （模拟繁殖）

1.  private int getLiveNum(int[][] board, int x, int y) {
2.         int c=0;
3.         for(int i=x-1; i<=x+1; i++) {
4.             for(int j=y-1; j<=y+1; j++) {
5.                 if(i<0 || j<0 || i>board.length-1 || j>board[0].length-1 || (i==x && j==y)) continue;
6.                 if(board[i][j]%10==1) c++;
7.             }
8.         }
9.         return c;
10.     }

[思路]

class Solution(object):
def cal(self,b,i,j):
#print b[i-1][j-1],b[i-1][j],b[i-1][j+1],',',b[i][j-1],b[i][j],b[i][j+1],',',b[i+1][j-1],b[i+1][j],b[i+1][j+1]
return b[i-1][j-1]+b[i-1][j]+b[i-1][j+1]+b[i][j-1]+b[i][j+1]+b[i+1][j-1]+b[i+1][j]+b[i+1][j+1]

def gameOfLife(self, board):
#print board

b = board

m = len(b)
if m == 0:
return
n = len(b[0])

b = [[0]*(n+2)]#list()#[[]]#[[0]]*n
#print b
for i in range(0,m):
#print board[i][:]
b.append([0])
b[i+1][:] += (board[i][:])
b[i+1] += [0]
# for j in board[i]:
#     b[i].append(j)
b.append([0]*(n+2))
#print b

for i in range(1,m+1):

for j in range(1,n+1):
cal = self.cal(b,i,j)
#print b[i][j],cal,' '
if b[i][j] == 1:
if cal < 2:
board[i-1][j-1] = 0
elif cal > 3:
board[i-1][j-1] = 0
else:
board[i-1][j-1] = b[i][j]
else:
if cal == 3:
board[i-1][j-1] = 1
else:
board[i-1][j-1] = b[i][j]

#print ''

#print board
"""
:type board: List[List[int]]
:rtype: void Do not return anything, modify board in-place instead.
"""



1
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：65947次
• 积分：2447
• 等级：
• 排名：第15223名
• 原创：187篇
• 转载：4篇
• 译文：0篇
• 评论：9条
阅读排行
最新评论