UVA457Linear Cellular Automata 题解

原题传送门

分析

直接按照题意模拟即可,开一个二维数组表示每天菌群密度数值,最后直接输出,注意题目要求的输出格式。具体实现见代码。

代码

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>

using namespace std;

int T;
int dna[11];
int bo[51][42];

void work() //依据题意模拟
{
    bo[0][20] = 1;
    for (int i = 1; i <= 50; i++)
    {
        for (int j = 1; j <= 40; j++)
        {
            bo[i][j] = dna[bo[i - 1][j - 1] + bo[i - 1][j] + bo[i - 1][j + 1]];
        }
    }
}

char change(int x) //菌群密度 数字->字符
{
    if (x == 0)
        return ' ';
    if (x == 1)
        return '.';
    if (x == 2)
        return 'x';
    if (x == 3)
        return 'W';
}

int main()
{
    scanf("%d", &T);
    for (int t = 0; t < T; t++)
    {
        if (t)
            cout << endl;
        memset(dna, 0, sizeof(dna));
        memset(bo, 0, sizeof(bo)); //以防万一清空
        for (int i = 0; i < 10; i++)
            scanf("%d", &dna[i]);

        work();
        for (int i = 0; i < 50; i++)
        {
            for (int j = 1; j <= 40; j++)
            {
                cout << change(bo[i][j]);
            }
            cout << endl;
        }
    }

    return 0;
}

AC记录

posted @ 2025-01-13 13:38  vanueber  阅读(25)  评论(0)    收藏  举报