289. Game of Life

According to the[Wikipedia's article]([[https://en.wikipedia.org/wiki/Conway's_Game_of_Life](https://en.wikipedia.org/wiki/Conway's_Game_of_Life)](https://en.wikipedia.org/wiki/Conway's_Game_of_Life](https://en.wikipedia.org/wiki/Conway's_Game_of_Life))\): "The Game of Life, also known simply as Life, is a cellular automaton devised by the British mathematician John Horton Conway in 1970."

Given aboard 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. The next state is created by applying the above rules simultaneously to every cell in the current state, where births and deaths occur simultaneously.





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. In place algorithm: each time encode the board element 2bit state code [next state, current state] and transition by shifting the board element after encoding.
  2. d


class Solution(object):
    def gameOfLife(self, board):
        :type board: List[List[int]]
        :rtype: void Do not return anything, modify board in-place instead.
        m = len(board)
        n = len(board[0])
        def countLives(board, m , n , i, j):
            lives = 0
            for x in range (max(i - 1, 0), min(i + 1, m - 1) + 1):
                for y in range(max(j - 1, 0), min(j + 1, n - 1) + 1):
                    # print('x: {}; y:{}; i: {}; j:{}; board[i][j]:{}, lives:{}'.format(x,y,i,j,board[i][j], lives))
                    lives += (board[x][y] & 1)
            lives -= (board[i][j] & 1)
            return lives

        for i in range(m):
            for j in range(n):
                lives = countLives(board, m, n, i, j)
                if board[i][j] and (lives == 2 or lives == 3):
                    board[i][j] = 3 #encode 11 
                if not board[i][j] and (lives == 3):
                    board[i][j] = 2
        for i in range(m):
            for j in range(n):
                board[i][j] >>= 1

Code ( infinite case)

class Solution(object):
    def gameOfLife(self, board):
        :type board: List[List[int]]
        :rtype: void Do not return anything, modify board in-place instead.
        # 1. find all the current alive cells
        # 2. update alive cells by counting the lives' neighbors appearance
        # 3. output results back to the board using alive cells
        live = {(i,j) for i, row in enumerate(board) for j, e in enumerate(row) if e}
        live = self.gameOfLifeInfinite(live)
        for i, row in enumerate(board):
            for j in range(len(row)):
                row[j] = int((i,j) in live) # a in-place solution for the board
    def gameOfLifeInfinite(self, live):
        ctr = collections.Counter((I,J)
                                 for i, j in live
                                 for I in range(i-1, i+2)
                                 for J in range(j-1, j+2)
                                 if I != i or J!= j)
        return {ij
                for ij in ctr
                if ctr[ij] == 3 or ctr[ij] == 2 and ij in live}

results matching ""

    No results matching ""