Title: Game of Life Source: leetcode.com
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):
- Any live cell with fewer than two live neighbors dies, as if caused by under-population.
- Any live cell with two or three live neighbors lives on to the next generation.
- Any live cell with more than three live neighbors dies, as if by over-population..
- 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:
- 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.
- 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?
Python (non-in-place) solution
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 44 45 46 47 48 |
''' https://leetcode.com/problems/game-of-life/ ''' class Solution(object): def getNextState(self, board, i, j, m, n): live_n = 0 if (i-1) >= 0: if (j-1) >= 0: live_n += board[i-1][j-1] live_n += board[i-1][j] if (j+1) < n: live_n += board[i-1][j+1] if (i+1) < m: if (j-1) >= 0: live_n += board[i+1][j-1] live_n += board[i+1][j] if (j+1) < n: live_n += board[i+1][j+1] if (j-1) >= 0: live_n += board[i][j-1] if (j+1) < n: live_n += board[i][j+1] res = board[i][j] if res == 0 and live_n == 3: res = 1 elif res == 1: if (live_n < 2) or (live_n > 3): res = 0 #print board[i][j], i, j, res, live_n return res 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]) board2=[] for i in range(m): board2.append([]) for j in range(n): board2[i].append(board[i][j]) for i in range(m): for j in range(n): board[i][j] = self.getNextState(board2, i, j, m, n) |
Python (in-place) solution
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 44 45 46 47 48 49 50 |
''' https://leetcode.com/problems/game-of-life/ ''' class Solution(object): def __init__(self): self.m = 0 self.n = 0 def getNextState(self, board, i, j): if i == self.m: return live_n = 0 if (i-1) >= 0: if (j-1) >= 0: live_n += board[i-1][j-1] live_n += board[i-1][j] if (j+1) < self.n: live_n += board[i-1][j+1] if (i+1) < self.m: if (j-1) >= 0: live_n += board[i+1][j-1] live_n += board[i+1][j] if (j+1) < self.n: live_n += board[i+1][j+1] if (j-1) >= 0: live_n += board[i][j-1] if (j+1) < self.n: live_n += board[i][j+1] res = board[i][j] if res == 0 and live_n == 3: res = 1 elif res == 1: if (live_n < 2) or (live_n > 3): res = 0 #print board[i][j], i, j, res, live_n if (j+1) < self.n: self.getNextState(board, i, j + 1) else: self.getNextState(board, i + 1, 0) board[i][j] = res def gameOfLife(self, board): """ :type board: List[List[int]] :rtype: void Do not return anything, modify board in-place instead. """ self.m = len(board) self.n = len(board[0]) if self.m == 0 or self.n==0: return self.getNextState(board, 0, 0) |
Java solution
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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
/* https://leetcode.com/problems/game-of-life/ */ class GameOfLife { public static void main(String args[]) { int[][] board = { {1, 1, 1}, {1, 0, 0}, {0, 0, 0} }; GameOfLife g = new GameOfLife(); g.gameOfLife(board); for(int[] row: board) { for(int col: row) System.out.print(col); System.out.println(); } } public void gameOfLife(int[][] board) { if(board.length!=0 && board[board.length-1].length!=0) getNextState(board, 0, 0); } public void getNextState(int [][] board, int i, int j) { int row = board.length; int col = board[board.length-1].length; int countLiveNeighbors = 0; int countDeadNeighbors = 0; int nextState = -1; if(i>row-1) return; if(i+1<row ) { if(board[i+1][j]==1)countLiveNeighbors++; } if(i-1>-1) { if(board[i-1][j]==1)countLiveNeighbors++; } if(j+1<col) { if(board[i][j+1]==1)countLiveNeighbors++; } if(j-1>-1) { if(board[i][j-1]==1)countLiveNeighbors++; } if(i-1>-1 && j-1 >-1) { if(board[i-1][j-1]==1)countLiveNeighbors++; } if(i+1<row && j-1 >-1) { if(board[i+1][j-1]==1)countLiveNeighbors++; } if(i-1>-1 && j+1 < col) { if(board[i-1][j+1]==1)countLiveNeighbors++; } if(i+1<row && j+1 < col) { if(board[i+1][j+1]==1)countLiveNeighbors++; } if(board[i][j]==1) { if(countLiveNeighbors<2) nextState = 0; else if(countLiveNeighbors >= 2 && countLiveNeighbors <= 3) nextState = 1; else if(countLiveNeighbors > 3) nextState = 0; } else { if(countLiveNeighbors==3) nextState = 1; else nextState = 0; } // System.out.println(nextState); if(j+1==col) getNextState(board, i+1, 0); else getNextState(board, i, j+1); board[i][j]=nextState; } } |