Home LeetCode - 289. Game of Life
Post
Cancel

LeetCode - 289. Game of Life

289. Game of Life - medium

문제

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
2
3
4
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.

제한사항

입출력 예

1
2
3
4
5
6
7
8
9
10
11
12
13
14
Input: 
[
  [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]
]

풀이

  • Array, Brute Force
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
class Solution {
public:
    void gameOfLife(vector<vector<int>>& board) {
        auto newBoard = board;
        int row = board.size();
        int col = board[0].size();
        
        for(auto i = 0 ; i < row ; ++i){
            for(auto j = 0 ; j < col ; ++j){
                
                // 현재 cell의 주변 이웃의 생존 및 죽은 cell 수 검사
                int liveCount = 0, deadCount = 0;
                for(auto m = -1 ; m < 2 ; ++m){
                    for(auto n = -1 ; n < 2 ; ++n){
                        if(m == 0 && n == 0)
                            continue;
                        if(i + m < 0 || i + m >= row)
                            continue;
                        if(j + n < 0 || j + n >= col)
                            continue;
                        
                        if(board[i + m][j + n])
                            ++liveCount;
                        else
                            ++deadCount;
                    }
                }
                
                // 현재 cell이 생존했을때,
                if(board[i][j]){
                    // 1번 조건
                    if(liveCount < 2)
                        newBoard[i][j] = 0;
                    // 2번 조건
                    else if(2 <= liveCount && liveCount <= 3)
                        newBoard[i][j] = 1;
                    // 3번 조건
                    else
                        newBoard[i][j] = 0;                        
                }
                // 현재 cell이 죽은상태일때,
                else{
                    // 4번 조건
                    if(liveCount == 3)
                        newBoard[i][j] = 1;
                }
            }
        }
        
        swap(newBoard, board);
    }
};
This post is licensed under CC BY 4.0 by the author.