本题比较基础,只是理解题意比较困难。
首先输入繁殖天数,即测试用例的个数;
然后输入每天的繁殖次数,每天繁殖这么多次后,得到结果矩阵;
然后输入一个数组D,代表每次繁殖的变换规则;
最后输入初始种群矩阵。
#include <iostream>
using namespace std;
int main()
{
int day; //天数
cin >> day;
while (day--)
{
int num; //繁殖次数
cin >> num;
int D[16];
int input[22][22], output[22][22];
memset(input, 0, sizeof(input));
memset(output, 0, sizeof(output));
for (int i = 0; i < 16; i++)
{
cin >> D[i]; //变换规则
}
for (int i = 1; i <= 20; i++)
{
for (int j = 1; j <= 20; j++)
{
cin >> input[i][j]; //初始种群
}
}
while (num--)
{
for (int i = 1; i <= 20; i++)
{
for (int j = 1; j <= 20; j++)
{
output[i][j] = input[i][j] + D[input[i][j] + input[i - 1][j] + input[i + 1][j] + input[i][j - 1] + input[i][j + 1]]; //每次繁殖的变换方式
if (output[i][j] > 3)
output[i][j] = 3;
else if (output[i][j] < 0)
output[i][j] = 0;
}
}
for (int i = 1; i <= 20; i++)
{
for (int j = 1; j <= 20; j++)
input[i][j] = output[i][j];
}
}
for (int i = 1; i <= 20; i++)
{
for (int j = 1; j <= 20; j++)
{
if (output[i][j] == 0)
cout << ".";
else if (output[i][j] == 1)
cout << "!";
else if (output[i][j] == 2)
cout << "X";
else
cout << "#";
}
cout << endl;
}
if (day > 0)
cout << endl;
}
return 0;
}
继续加油。
来源:https://blog.csdn.net/Intelligence1028/article/details/102748745