C++ 生命游戏

生命游戏

题目

一个二维方格世界,每个方格只能存活一个细胞。每个细胞在下一个时刻是否存活取决于这一个时刻相邻八个方格中细胞的总数量,每个空格在下一个时刻是否产生细胞也取决于这一个时刻相邻八个方格中细胞的总数量,规则如下:

  1. 如果一个空格周围有且只有3个活着的细胞,则该空格产生一个细胞。
  2. 如果一个细胞周围有且只有2个或3个活着的细胞,则该细胞继续存活。
  3. 在其它情况下,细胞要么拥挤而死,要么孤独而死。

样例

输入

49 20
.................................................
.................................................
.................................................
.......................OOO.......................
.................................................
........................O........................
........................O........................
........................O........................
.................................................
.................................................
.................................................
.................................................
.................................................
.................................................
.................................................
.................................................
.................................................
.................................................
.................................................
.................................................
 

提示

编译前下载头文件util.h

#include <bits/stdc++.h>
#include "util.h"
using namespace std;

int main()
{
    /*
        TODO
    */

    clear();
    for (int i = 0; true; i++) {
        pause(100);
        recursor();
        for (int y = 0; y < CELLS_HEIGHT; y++) {
            for (int x = 0; x < CELLS_WIDTH; x++) {
                if (cells[y][x]) cout << 'O';
                else cout << ' ';
            }
            cout << '\n';
        }

        /*
            TODO
        */
    }
    return 0;
}

答案

#include <bits/stdc++.h>
#include "util.h"
using namespace std;

int main() {
    freopen("in.txt", "r", stdin);
    int CELLS_WIDTH, CELLS_HEIGHT;
    cin >> CELLS_WIDTH >> CELLS_HEIGHT;
    auto cells = vector<vector<bool>>(CELLS_HEIGHT, vector<bool>(CELLS_WIDTH, false));
    for (int y = 0; y < CELLS_HEIGHT; y++) {
        for (int x = 0; x < CELLS_WIDTH; x++) {
            char c;
            cin >> c;
            if (c == 'O') cells[y][x] = true;
        }
    }

    clear();
    for (int i = 0; true; i++) {
        pause(100);
        recursor();
        for (int y = 0; y < CELLS_HEIGHT; y++) {
            for (int x = 0; x < CELLS_WIDTH; x++) {
                if (cells[y][x]) cout << 'O';
                else cout << ' ';
            }
            cout << '\n';
        }

        auto next_cells = vector<vector<bool>>(CELLS_HEIGHT, vector<bool>(CELLS_WIDTH, false));
        for (int y = 0; y < CELLS_HEIGHT; y++) {
            for (int x = 0; x < CELLS_WIDTH; x++) {
                int count = 0;

                if (y == 0 && x == 0) {
                    if (cells[y][x + 1]) count++;
                    if (cells[y + 1][x]) count++;
                    if (cells[y + 1][x + 1])count++;
                }
                else if (y == 0 && x != 0 && x != CELLS_WIDTH - 1) {
                    if (cells[y][x - 1]) count++;
                    if (cells[y][x + 1]) count++;
                    if (cells[y + 1][x - 1]) count++;
                    if (cells[y + 1][x]) count++;
                    if (cells[y + 1][x + 1]) count++;
                }
                else if (y == 0 && x == CELLS_WIDTH - 1) {
                    if (cells[y][x - 1]) count++;
                    if (cells[y + 1][x - 1]) count++;
                    if (cells[y + 1][x]) count++;
                }
                else if (y != 0 && y != CELLS_HEIGHT - 1 && x == 0) {
                    if (cells[y - 1][x]) count++;
                    if (cells[y - 1][x + 1]) count++;
                    if (cells[y][x + 1]) count++;
                    if (cells[y + 1][x]) count++;
                    if (cells[y + 1][x + 1]) count++;
                }
                else if (y == CELLS_HEIGHT - 1 && x == 0) {
                    if (cells[y - 1][x]) count++;
                    if (cells[y - 1][x + 1]) count++;
                    if (cells[y][x + 1]) count++;
                }
                else if (y == CELLS_HEIGHT - 1 && x != 0 && x != CELLS_WIDTH - 1) {
                    if (cells[y - 1][x - 1]) count++;
                    if (cells[y - 1][x]) count++;
                    if (cells[y - 1][x + 1]) count++;
                    if (cells[y][x - 1]) count++;
                    if (cells[y][x + 1]) count++;
                }
                else if (x == CELLS_WIDTH - 1 && y != 0 && y != CELLS_HEIGHT - 1) {
                    if (cells[y - 1][x - 1]) count++;
                    if (cells[y - 1][x]) count++;
                    if (cells[y][x - 1]) count++;
                    if (cells[y + 1][x - 1]) count++;
                    if (cells[y + 1][x]) count++;
                }
                else if (y == CELLS_HEIGHT - 1 && x == CELLS_WIDTH - 1) {
                    if (cells[y - 1][x - 1]) count++;
                    if (cells[y - 1][x]) count++;
                    if (cells[y][x - 1]) count++;
                }
                else if (y != 0 && x != 0 && y != CELLS_HEIGHT - 1 && x != CELLS_WIDTH - 1) {
                    if (cells[y - 1][x - 1]) count++;
                    if (cells[y - 1][x]) count++;
                    if (cells[y - 1][x + 1]) count++;
                    if (cells[y][x - 1]) count++;
                    if (cells[y][x + 1]) count++;
                    if (cells[y + 1][x - 1]) count++;
                    if (cells[y + 1][x]) count++;
                    if (cells[y + 1][x + 1]) count++;
                }

                if (count <= 1 || count >= 4) next_cells[y][x] = false;
                else if (count == 3) next_cells[y][x] = true;
                else next_cells[y][x] = cells[y][x];
            }
        }
        cells = next_cells;
    }
    return 0;
}

  • 22
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的 C++ 生命游戏代码: ```c++ #include <iostream> #include <cstdlib> #include <ctime> using namespace std; const int ROW = 20; const int COL = 20; void init_board(int board[][COL]) { srand(time(nullptr)); for (int i = 0; i < ROW; i++) { for (int j = 0; j < COL; j++) { board[i][j] = rand() % 2; } } } void print_board(int board[][COL]) { for (int i = 0; i < ROW; i++) { for (int j = 0; j < COL; j++) { if (board[i][j] == 1) { cout << "* "; } else { cout << ". "; } } cout << endl; } } int count_neighbors(int board[][COL], int row, int col) { int count = 0; for (int i = -1; i <= 1; i++) { for (int j = -1; j <= 1; j++) { if (i == 0 && j == 0) { continue; } int r = row + i; int c = col + j; if (r < 0 || r >= ROW || c < 0 || c >= COL) { continue; } count += board[r][c]; } } return count; } void update_board(int board[][COL]) { int new_board[ROW][COL] = {0}; for (int i = 0; i < ROW; i++) { for (int j = 0; j < COL; j++) { int count = count_neighbors(board, i, j); if (board[i][j] == 1) { if (count < 2) { new_board[i][j] = 0; } else if (count == 2 || count == 3) { new_board[i][j] = 1; } else { new_board[i][j] = 0; } } else { if (count == 3) { new_board[i][j] = 1; } else { new_board[i][j] = 0; } } } } for (int i = 0; i < ROW; i++) { for (int j = 0; j < COL; j++) { board[i][j] = new_board[i][j]; } } } int main() { int board[ROW][COL] = {0}; init_board(board); for (int i = 0; i < 10; i++) { print_board(board); update_board(board); cout << endl; } return 0; } ``` 该代码使用了二维数组来表示生命游戏的棋盘,使用随机数来初始化棋盘中的细胞。`count_neighbors` 函数用于计算某个细胞周围的存活细胞数量,`update_board` 函数用于更新棋盘中的细胞状态。最后,`main` 函数使用循环调用 `print_board` 和 `update_board` 函数来模拟生命游戏的演化过程。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值