289. Game of Life

按照 规则 修改二维数组元素值。

二维数组中只有1和0, 1表示live, 2表示dead.

规则如下:

1. 一个存活的cell,它有小于两个邻居dead,那么它在下一代die.

2. 一个存活的cell,它有2到3个邻居live,那么它在下一代也live.

3. 一个存活的cell,它有多于3邻居die,那么它在下一代也die.

4. 一个dead的cell,它有3个邻居live,那么它在下一代也live.

注意:数组中的每个元素同时进入下一代,没有先后顺序问题。

思考:题目的重点在于不能修改一个元素,用修改后的结果影响到下一代。解决方法有两种,(方法1)使用辅助空间,分两步走,先统计每个元素的邻居的个数放在辅助空间中,再根据记录的邻居个数,更新原数组。(方法2)不需要辅助空间,直接在原数组上修改,用非0和1元素替代,这样就不会和原始元素值混淆了。

解法:   不用辅助空间

   void gameOfLife(vector<vector<int>>& board) {
        int rows = board.size();
        int cols = board[0].size();
        
        int neighbors[3] = {0, 1, -1};
        
        // live -> die -1
        // die -> live 2
        for (int r = 0; r < rows; ++r) {
            for (int c = 0; c < cols; ++c) {
                
                int live_count = 0;
                for (int i = 0; i < 3; ++i) {
                    for (int j = 0; j < 3; ++j) {
                        if (!(neighbors[i] == 0 && neighbors[j] ==0)) {
                            int n_r = r + neighbors[i];
                            int n_c = c + neighbors[j];
                            if ((n_r >= 0 && n_r < rows) &&
                               (n_c >= 0 && n_c < cols) &&
                               abs(board[n_r][n_c]) == 1) {
                                ++live_count;
                            }
                        }
                    }
                }
                
                if (board[r][c] == 1 && (live_count < 2 || live_count > 3)) {
                    board[r][c] = -1;
                } 
                if (board[r][c] == 0 && live_count == 3) {
                    board[r][c] = 2;
                }
            }
        }

        for (int r = 0; r < rows; ++r) {
            for (int c = 0; c < cols; ++c) {
                if (board[r][c] == -1) {
                    board[r][c] = 0;
                } else if (board[r][c] == 2) {
                    board[r][c] = 1;
                    
                }
            }
        }
        
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是Game of Life的C++代码示例: ```cpp #include <iostream> #include <vector> #include <chrono> #include <thread> #include <cstdlib> using namespace std; class GameOfLife { private: vector<vector<int>> grid; int rows, columns; public: GameOfLife(int r, int c) : rows(r), columns(c) { grid.resize(rows, vector<int>(columns, 0)); } void initialize() { srand(time(NULL)); for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { grid[i][j] = rand() % 2; } } } int getNeighbourCount(int row, int col) { int count = 0; for (int i = -1; i <= 1; i++) { for (int j = -1; j <= 1; j++) { int r = row + i; int c = col + j; if (r >= 0 && r < rows && c >= 0 && c < columns && !(i == 0 && j == 0)) { count += grid[r][c]; } } } return count; } void nextGeneration() { vector<vector<int>> newGrid(rows, vector<int>(columns, 0)); for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { int neighbourCount = getNeighbourCount(i, j); if (grid[i][j] == 0 && neighbourCount == 3) { newGrid[i][j] = 1; } else if (grid[i][j] == 1 && (neighbourCount == 2 || neighbourCount == 3)) { newGrid[i][j] = 1; } else { newGrid[i][j] = 0; } } } grid = newGrid; } void print() { for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { if (grid[i][j] == 1) { cout << "* "; } else { cout << ". "; } } cout << endl; } } }; int main() { int rows = 40, columns = 40; GameOfLife gol(rows, columns); gol.initialize(); while (true) { gol.print(); gol.nextGeneration(); this_thread::sleep_for(chrono::milliseconds(100)); system("clear"); } return 0; } ``` 此代码实现了一个Game of Life模拟器,它使用随机值初始化单元格,并在每次迭代中计算每个单元格周围的邻居数量,然后使用规则进行更新。最后,它在控制台中打印游戏板并在每次迭代后清除屏幕。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值