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

原创 2015年11月18日 20:25:14

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.

Follow up

  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.     }  


[思路]

inplace的话,   只要有办法区分 4种状态,   DEAD->LIVE, DEAD->DEAD, LIVE->LIVE, LIVE->DEAD 即可.   int 完全可以找4个数来表示这4种状态.  






       

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.
        """
       

【LeetCode】Game of Life

Game of Life My Submissions Question Solution  Total Accepted: 3524 Total Submissions: 11205 Diffi...
  • u013027996
  • u013027996
  • 2015年10月14日 10:48
  • 1699

[leetcode 289]Game of Life

According to the Wikipedia's article: "The Game of Life, also known simply as Life, is a cellular a...
  • ER_Plough
  • ER_Plough
  • 2015年10月07日 13:52
  • 1409

[leetcode-289]Game of Life(java)

问题描述:分析:这道题起初自己搞复杂了,我原先以为是求最后的稳态,即一个状态再跳转到另外一个状态时,两个矩阵是一样的。但是有个测试用例是: 0 0 0 0 0 0 0 1 0 0 0 0 1 0...
  • zdavb
  • zdavb
  • 2015年10月04日 11:22
  • 1514

[leetcode] 289. Game of Life 解题报告

题目链接: https://leetcode.com/problems/game-of-life/ According to the Wikipedia's article: "The Ga...
  • qq508618087
  • qq508618087
  • 2016年05月09日 11:46
  • 1211

LeetCode 289 Game of Life

题目描述According to the Wikipedia’s article: “The Game of Life, also known simply as Life, is a cellula...
  • Yano_nankai
  • Yano_nankai
  • 2015年12月14日 10:15
  • 976

leetcode 289: Game of Life

Game of Life Total Accepted: 606 Total Submissions: 1907 Difficulty: Medium Accordin...
  • xudli
  • xudli
  • 2015年10月04日 15:19
  • 7791

LeetCode- 289. Game of Life - 思路详解-C++

假设有一个大小为m*n的板子,有m行,n列个细胞。每个细胞有一个初始的状态,死亡或者存活。每个细胞和它的邻居。垂直,水平以及对角线。按照规则互动,计算下一状态。...
  • SJWL2012
  • SJWL2012
  • 2017年01月11日 16:14
  • 543

leetcode笔记:Game of Life

根据维基百科条目 Conway's Game of Life(康威生命游戏),康威生命游戏是英国数学家约翰·何顿·康威在1970年发明的细胞自动机。...
  • liyuefeilong
  • liyuefeilong
  • 2016年02月18日 00:06
  • 1655

Conway’s Game of Life中看C++SSE2并行化计算

一、Conway’s Game of Life描述 康威生命游戏(英语:Conway's Game of Life),又称康威生命棋,是英国数学家约翰·何顿·康威在1970年发明的细胞自动机。   ...
  • sinat_34463016
  • sinat_34463016
  • 2017年01月08日 16:20
  • 439

LeetCode 289. Game of Life(生命游戏)

原题网址:https://leetcode.com/problems/game-of-life/ According to the Wikipedia's article: "The Game ...
  • jmspan
  • jmspan
  • 2016年04月16日 00:20
  • 375
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Game of Life 【leetcode】【98.9】【M】
举报原因:
原因补充:

(最多只允许输入30个字)