永恒的生命游戏

https://iai.sh.cn/problem/42
#include <iostream>
#include <vector>
using namespace std;

int main()
{

    int row = 0, col = 0;
    char chStarPoint[3];
    bool blStill = true;

    chStarPoint[1] = '*';
    chStarPoint[2] = '.';

    cin >> row;
    cin >> col;
    string strRow;

    vector<vector<unsigned char>> c(row + 1);
    vector<vector<unsigned char>> n(row + 1);

    for (int i = 0; i <= row; i++)
    {
        c[i].resize(col + 1);
        n[i].resize(col + 1);
    }

    // set use the cin to update the mine matrix
    for (int i = 0; i < row; i++)
    {
        cin >> strRow;
        for (int k = 0; k < col; k++)
        {
            c[i][k] = strRow[k];
        }
    }

    for (int i = 0; i < row; i++)
    {
        for (int k = 0; k < col; k++)
        {

            int countStar = 0;
            // column -1
            if ((i > 0 and k > 0) and c[i - 1][k - 1] == chStarPoint[1])
                countStar++;
            if ((k > 0) and c[i][k - 1] == chStarPoint[1])
                countStar++;
            if ((k > 0) and c[i + 1][k - 1] == chStarPoint[1])
                countStar++;

            // column 0
            if ((i > 0) and c[i - 1][k] == chStarPoint[1])
                countStar++;

            // if (chDetail[i][k] == chStarPoint[1]) countStar++; //just keep format, no use
            if (c[i + 1][k] == chStarPoint[1])
                countStar++;
            // column +1

            if ((i > 0) and c[i - 1][k + 1] == chStarPoint[1])
                countStar++;
            if (c[i][k + 1] == chStarPoint[1])
                countStar++;
            if (c[i + 1][k + 1] == chStarPoint[1])
                countStar++;

            switch (c[i][k])
            {
            case '.':
                if (countStar == 3)
                {
                    n[i][k] = chStarPoint[1];
                }
                else
                {
                    n[i][k] = chStarPoint[2];
                }
                break;
            case '*':
                if ((countStar == 2) or (countStar == 3))
                {
                    n[i][k] = chStarPoint[1];
                }
                else
                {
                    n[i][k] = chStarPoint[2];
                };
                break;
            }
        }
    }

    for (int i = 0; i < row; i++)
    {
        for (int k = 0; k < col; k++)
        {
            if (c[i][k] != n[i][k])
                blStill = false;
        }
    }

    if (blStill)
    {
        cout << "Still life" << endl;
    }
    else
    {
        cout << "Other" << endl;
    }

    return 0;
}

已发布

分类

来自

标签: