Game of Life -- leetcode

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.

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?

题目大意为:

1. 用1表示生,0表示死

2. 如果当前为生状态,周围8个邻居中,如果有2个或者3个为生状态,下一轮继续为生。

3. 如果当前为死状态,周围8个邻居中,如果有3个为生状态,下一轮为生。


解法一:

单元格为int类型。共32个bit位。

使用第0位,表示当前生死状态。

使用第1位,表示下一轮的生死状态。

class Solution {
public:
    void gameOfLife(vector<vector<int>>& board) {
        if (board.empty()) return;
        
        for (int i=0; i<board.size(); i++) {
            for (int j=0; j<board[i].size(); j++) {
                board[i][j] = (getNewState(board, i, j) << 1) + board[i][j];
            }
        }
        
        for (int i=0; i<board.size(); i++) {
            for (int j=0; j<board[i].size(); j++) {
                board[i][j] >>= 1;
            }
        }
    }
    
    int getNewState(vector<vector<int>>& board, int x, int y) {
        int sum = 0;
        for (int i=max(x-1,0); i<=min(x+1, (int)board.size()-1); i++) {
            for (int j=max(y-1,0); j<=min(y+1, (int)board[i].size()-1); j++) {
                sum += board[i][j] & 1;
            }
        }
        
        sum -= board[x][y];
        
        if (sum ==3 || sum == 2 && board[x][y])
            return 1;
        else
            return 0;
    }
};


解法二:

单元格类型为int类型。

使用bit位0,表示当前的生死状态。

使用其他位表示,状态为生的邻居总数。


下面算法中+2, 表示使用第1位~第31位,作为计数器

如果使用第2位~第31位,作计数器,则需要+4.


另一点,在计算当前结点,所有活着的邻居总数时,同时,也对未处理的邻居,进行了计算。

这样做的好处是,省去了对已经处理过的节点进行引用。从而在一趟中,就能得出下一轮的状态值。


对比,

另一种做法是,处理每一个单元格时,对周围8个邻居,进行状态为生的统计。并将统计结果存入高位。

在第二趟扫描,根据第一趟的结果更新最终的生死状态。


class Solution {
public:
    void gameOfLife(vector<vector<int>>& board) {
        if (board.empty()) return;
        
        for (int i=0; i<board.size(); i++) {
            for (int j=0; j<board[i].size(); j++) {
                addNeighbor(board, i, j, i, j+1);
                addNeighbor(board, i, j, i+1, j-1);
                addNeighbor(board, i, j, i+1, j);
                addNeighbor(board, i, j, i+1, j+1);
                
                if (board[i][j] >= 5 && board[i][j] <= 7)
                    board[i][j] = 1;
                else
                    board[i][j] = 0;
            }
        }
    }
    
    void addNeighbor(vector<vector<int>>& board, int i, int j, int x, int y)
    {
        if (x < 0 || y < 0 || x == board.size() || y == board[x].size())
            return;
            
        if (board[x][y] & 1)
            board[i][j] += 2;
        if (board[i][j] & 1)
            board[x][y] += 2;
    }
};



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值