[leetcode]289. Game of Life

本文深入解析LeetCode上编号为289的“生命游戏”问题,探讨了根据细胞自动机原理更新二维网格状态的两种算法实现。一种方法通过开辟新数组进行状态转移,而另一种则采用位运算优化空间复杂度至O(1),实现了高效的生命状态演化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[leetcode]289. Game of Life


Analysis

2019年还没见过太阳,等一个晴天!!!—— [每天刷题并不难0.0]

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.
    在这里插入图片描述

Explanation:

题目意思比较简单,就是根据邻居中存活的的人数来判断每个点是否会继续存活下去或者死亡

Implement

solution 1: (这个是直接新开了一个数组,然后判断的)

class Solution {
public:
    void gameOfLife(vector<vector<int>>& board) {
        vector<vector<int>> tmp(board);
        int len = board.size();
        int len1 = board[0].size();
        for(int i=0; i<len; i++){
            for(int j=0; j<len1; j++){
                int cnt = 0;
                for(auto d:dir){
                    int x=i+d[0], y=j+d[1];
                    if(alive(tmp, x, y, len, len1))
                        cnt++;
                }
                if((cnt < 2 || cnt >3) && board[i][j])
                    board[i][j] = 0;
                else if(cnt == 3 && board[i][j]==0)
                    board[i][j] = 1;
            }
        }
    }
    bool alive(vector<vector<int>>& tmp, int x, int y, int len, int len1){
        if(x>=0 && x<len && y>=0 && y<len1 && tmp[x][y])
            return true;
        return false;
    }
private:
    int dir[8][2] = {{1, 0}, {1, 1}, {1, -1}, {0, 1}, {0, -1}, {-1, 0}, {-1, 1}, {-1, -1}};
};

solution 2 (space complexity: O(1))
这个方法是没有另外开通数组,而是直接利用位运算符,每个位置的第0位表示当前状态,第一位表示更新后的状态,然后最后把每个点的状态右移一位就可以更新得到新的状态了~

class Solution {
public:
    void gameOfLife(vector<vector<int>>& board) {
        int m = board.size();
        int n = board[0].size();
        for(int i=0; i<m; i++){
            for(int j=0; j<n; j++){
                int cnt = 0;
                for(auto d:dir){
                    int x=i+d[0], y=j+d[1];
                    if(valid(x, y, m, n)){
                        if(board[x][y] & 1)
                            cnt++;
                    }
                }
                if(board[i][j] == 0){
                    if(cnt == 3)
                        board[i][j] |= 2;
                }
                else{
                    if(cnt < 2 || cnt > 3)
                        board[i][j] &= 1;
                    else
                        board[i][j] |= 2;
                }
            }
        }
        for(int i=0; i<m; i++){
            for(int j=0; j<n; j++)
                board[i][j] >>= 1;
        }
    }
    bool valid(int x, int y, int len, int len1){
        if(x>=0 && x<len && y>=0 && y<len1)
            return true;
        return false;
    }
private:
    int dir[8][2] = {{1, 0}, {1, 1}, {1, -1}, {0, 1}, {0, -1}, {-1, 0}, {-1, 1}, {-1, -1}};
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值