[LeetCode 289] Game of Life

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. 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.

Example:

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]
]

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的解决这个问题,需要考虑如果在同一个数组中既要记录现在的状态,又要记录将来的状态。

例如如果 1->0的变化,我们如果直接将1 置成0,那么live的状态就会丢失了,之后的遍历就拿不到他的这一时刻的状态。所以我们需要引入另外两种状态, -1和2,-1代表0->1,即dead的cell 需要转化为live,但是还没有转变;2代表1->0,即live的cell即将要转换为0,但是还没有转变。

这样之后的遍历在计算live neighbor cell的总数时就需要判断附近状态为1和2的总数,计算dead neighbor cell的总数时需要判断附近状态为0和-1的总数。

Code

class Solution {
public:
    void gameOfLife(vector<vector<int>>& board) {
        int row = board.size();
        if (row == 0)
            return;
        int col = board[0].size();
        
        for (int i = 0; i < row; i ++)
        {
            for (int j = 0; j < col; j ++)
            {
                nextState(board, i, j);
            }
        }
        
        for (int i = 0; i < row; i ++)
            for (int j = 0; j < col; j++)
            {
                if(board[i][j] == -1)
                    board[i][j] = 1;
                else if (board[i][j] == 2)
                    board[i][j] = 0;
            }
        return;
    }
    
    void nextState(vector<vector<int>>& board, int x, int y)
    {
        int liveSum = 0;
        int deadSum = 0;
        
        liveSum = isLive(board, x-1, y-1) + isLive(board, x-1, y)
            + isLive(board, x-1, y+1) + isLive(board, x, y-1)
            + isLive(board, x, y+1) + isLive(board, x+1, y-1)
            + isLive(board, x+1, y) + isLive(board, x+1, y+1);
        deadSum = isDead(board, x-1, y -1) + isDead(board, x-1, y)
            + isDead(board, x-1, y+1) + isDead(board, x, y-1)
            + isDead(board, x, y+1) + isDead(board, x+1, y-1)
            + isDead(board, x+1, y) + isDead(board, x+1, y+1);
        if (board[x][y] == 1)
        {
            if(liveSum < 2 || liveSum > 3)
                board[x][y] = 2;
        }
        else
        {
            if (liveSum == 3)
                board[x][y] = -1;
        }
        return;
    }
    
    int isLive(vector<vector<int>>& board, int x, int y)
    {
        int row = board.size();
        int col = board[0].size();
        if (x < 0 || y < 0 || x >= row || y >= col)
            return 0;
        if (board[x][y] == 1 || board[x][y] == 2)
            return 1;
        return 0;
    }
    int isDead(vector<vector<int>>& board, int x, int y)
    {
        int row = board.size();
        int col = board[0].size();
        
        if (x < 0 || y < 0 || x >= row || y >= col)
            return 0;
        if (board[x][y] == 0 || board[x][y] == -1)
            return 1;
        return 0;
    }
};

运行效率

Runtime: 4 ms, faster than 100.00% of C++ online submissions for Game of Life.

Memory Usage: 8.5 MB, less than 100.00% of C++ online submissions for Game of Life.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值