【LeetCode从零单刷】Game of Life

来源:互联网 发布:卖家淘宝小二在线联系 编辑:程序博客网 时间:2024/05/20 21:21

题目:

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) ordead (0). Each cell interacts with its eight neighbors (horizontal, vertical, diagonal) using the following four rules (taken from the above Wikipedia article):

  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.

Follow up:

  1. 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.
  2. 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两种状态不够,我们需要定义更多的状态:

  • 0:0-》0
  • 1:1-》1
  • 2:0-》1
  • 3:1-》0

除此之外,我还想特别说一个问题:邻域检测,尤其当不知道邻域元素个数的情况(图像处理中尤其常见)。

我以前比较笨的方法单独处理各种情况:角落元素、边界非角落元素、非边界元素……非常麻烦而且容易错。

正确的处理方法:循环访问,但是循环范围起始点可变。每次计算 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;            }        }    }};

0 0
原创粉丝点击