Game Of Life Problem


Description

LeetCode Problem 289.

According to 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.”

The board is made up of an m x n grid of cells, where each cell has an initial state: live (represented by a 1) or dead (represented by a 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.

The next state is created by applying the above rules simultaneously to every cell in the current state, where births and deaths occur simultaneously. Given the current state of the m x n grid board, return the next state.

Example 1:

1
2
Input: board = [[0,1,0],[0,0,1],[1,1,1],[0,0,0]]
Output: [[0,0,0],[1,0,1],[0,1,1],[0,1,0]]

Example 2:

1
2
Input: board = [[1,1],[1,0]]
Output: [[1,1],[1,1]]

Constraints:

  • m == board.length
  • n == board[i].length
  • 1 <= m, n <= 25
  • board[i][j] is 0 or 1.


Sample C++ Code

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
class Solution {
public:
    void gameOfLife(vector<vector<int>>& board) {
        int m = board.size();
        if (m == 0) return;
        int n = board[0].size();
        
        vector<vector<int>> dir = { {-1,-1}, {-1,0}, {-1,1}, 
                                   {0,-1}, {0,1}, {1,-1},
                                   {1,0}, {1,1} };
        
        int x, y, cnt_live, cnt_dead;
        for (int i = 0; i < m; i ++) {
            for (int j = 0; j < n; j ++) {
                cnt_live = 0, cnt_dead = 0;
                for (int k = 0; k < 8; k ++) {
                    x = i + dir[k][0];
                    y = j + dir[k][1];
                    if ((x >= 0) && (x < m) && (y >= 0) && (y < n)) {
                        if (abs(board[x][y]) == 1)
                            cnt_live ++;
                        else
                            cnt_dead ++;
                    }
                }
                if (board[i][j] == 1) {
                    if (cnt_live < 2)
                        board[i][j] = -1;
                    else if (cnt_live > 3)
                        board[i][j] = -1;
                } else {
                    if (cnt_live == 3)
                        board[i][j] = 2;
                }
            }
        }
        for (int i = 0; i < m; i ++) 
            for (int j = 0; j < n; j ++)
                board[i][j] = (board[i][j] <= 0) ? 0 : 1;
        return;
    }
};




Related Posts

Walking Robot Simulation Problem

LeetCode 874. A robot on an infinite XY-plane starts at...

Valid Mountain Array Problem

LeetCode 941. Given an array of integers arr, return true...

Sum Of Even Numbers After Queries Problem

LeetCode 985. You are given an integer array nums and...

Partition Array Into Disjoint Intervals Problem

LeetCode 915. Given an integer array nums, partition it into...

Monotonic Array Problem

LeetCode 896. An array is monotonic if it is either...

Maximize Distance To Closest Person Problem

LeetCode 849. You are given an array representing a row...