【LeetCodeゼロブラシから】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 board with m by n cells, each cell has an initial statelive (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?

  • 回答:
    この問題の鍵はin-placeにあり,定数レベルの新しい空間しか開かない.従って、既存の状態は、情報量を増加させるために中間情報フォーマットを設計する前の状態の情報を含む必要がある.最後に、中間情報フォーマットを通常フォーマットに復号する.時間を空間に変える方法です.
    したがって、0、1の2つのステータスが不足しているため、より多くのステータスを定義する必要があります.
  • 0:0-》0
  • 1:1-》1
  • 2:0-》1
  • 3:1-》0

  • それ以外に、特に1つの問題を言いたいのは、隣接領域検出、特に隣接領域要素の個数が分からない場合(画像処理では特によく見られる).
    私は以前、コーナー要素、境界非コーナー要素、非境界要素など、不器用な方法でいろいろな状況を単独で処理していましたが、とても面倒で間違いやすいです.
    正しい処理方法:ループアクセスですが、ループ範囲の開始点は可変です.毎回minrow~maxrow,mincol~maxcolを計算します.注意:要素自体を計算しないでください.
    class Solution {
    public:
    
        int next(int i, int j, vector<vector<int>> board, int cur)
        {
            int rowsize = board.size();
            int colsize = board[0].size();
            
            int minrow = i > 0 ? (i-1) : 0;
            int maxrow = i < rowsize - 1 ? (i+1) : (rowsize-1);
            int mincol = j > 0 ? (j-1) : 0;
            int maxcol = j < colsize - 1 ? (j+1) : (colsize-1);
            
            int neighborlive = 0;
            for(int a = minrow; a <= maxrow; a++){
                for(int b = mincol; b <= maxcol; b++){
                    if(a == i && b == j)	continue;
                    if(board[a][b] == 1 || board[a][b] == 3) neighborlive++;
                }
            }
            
            if(cur == 1){
                if(neighborlive < 2 || neighborlive > 3) return 3;
                else return 1;
            }
            else{
                if(neighborlive ==3) return 2;
                else return 0;
            }
        }
    
        void gameOfLife(vector<vector<int>>& board) {
            int rowsize = board.size();
            int colsize = board[0].size();
            
            // encoding
            for(int i = 0; i< rowsize; i++){
                for(int j = 0; j< colsize; j++){
                    board[i][j] = next(i, j, board, board[i][j]);
                }
            }
            
            // decoding
            for(int i = 0; i< rowsize; i++){
                for(int j = 0; j< colsize; j++){
                    if (board[i][j] == 3) board[i][j] = 0;
                    if (board[i][j] == 2) board[i][j] = 1;
                }
            }
        }
    };