## Game of Life

According to the Wikipedia’s article: “The

Game of Life, also known simply asLife, is a cellular automaton devised by the British mathematician John Horton Conway in 1970.”Given a

boardwithmbyncells, each cell has an initial statelive(1) ordead(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?

Credits:

Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.

O(nm) space O(nm) time

class Solution { public: void gameOfLife(vector<vector<int>>& board) { vector<vector<int> > next = board; if(board.size() == 0 || board[0].size() == 0) return ; for(int r = 0; r < board.size(); r++){ for(int c = 0; c < board[0].size(); c++){ int n = count(board, r, c); int v; if(n < 2 || n > 3){ v = 0; } else if(n == 3){ v = 1; } else{//remain unchanged v = board[r][c]; } next[r][c] = v; } } board = next; } int count(vector<vector<int> >& board, int r, int c){ int direct[][2] = {{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1}}; int ans = 0; for(int i = 0; i < 8; i++){ int nr = r + direct[i][0]; int nc = c + direct[i][1]; if(nr < 0 || nr >= board.size() || nc < 0 || nc >= board[0].size()){ continue; } ans += board[nr][nc]; } return ans; } };

Inorder to solve this problem in space, we notice that we only store 0 and 1, two states within the integer variable, which is a waste of memory.

So we can utilize the integer to store the num of alive neighbours in it. To distinguish original state (alive or not) of the cell, we flip it to negative if it’s dead before, we add the num of alive neighbours by 1 to indicate it’s alive before.

Adding 1 is to separate dead state and alive state in case num is 0.

So if n > 0 it’s alive before, otherwise it’s dead.

O(1) space, O(nm) time

class Solution { public: void gameOfLife(vector<vector<int>>& board) { if(board.size() == 0 || board[0].size() == 0) return ; for(int r = 0; r < board.size(); r++){ for(int c = 0; c < board[0].size(); c++){ int n = count(board, r, c); if(board[r][c] == 1){ //alive before, plus 1 here in case n is 0, and we can't distinguish alive and dead board[r][c] = 1 + n; } else{ //dead before, board <= 0 board[r][c] = -1 * n; } } } for(int r = 0; r < board.size(); r++){ for(int c = 0; c < board[0].size(); c++){ int n = board[r][c]; int prevVal; int v; if(n > 0){ n -= 1; prevVal = 1; } else{ n *= -1; prevVal = 0; } if(n < 2 || n > 3){ v = 0; } else if(n == 3){ v = 1; } else{ //abs(n) == 2 v = prevVal; } board[r][c] = v; } } } int count(vector<vector<int> >& board, int r, int c){ int direct[][2] = {{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1}}; int ans = 0; for(int i = 0; i < 8; i++){ int nr = r + direct[i][0]; int nc = c + direct[i][1]; if(nr < 0 || nr >= board.size() || nc < 0 || nc >= board[0].size()){ continue; } ans += board[nr][nc] > 0? 1: 0; } return ans; } };