把数组中的一个数,与其上下左右的四个数相加,得到的数值对应到DNA序列里面的对应数,把这个对应数加到原数组的对应位置,
然后超过三的为3,低于零的为0,处理后,即为一天的变化情况。
比如:
3 0 0 3 3 0 4 1 0 3 1 0
0 0 0 —— 3 0 0 —— 1 0 0 —— 1 0 0 这就是第一天的繁殖密度
0 0 0 0 0 0 0 0 0 0 0 0
代码如下:
#include <iostream>
#include <string.h>
using namespace std;
const int DishSize = 20; //培养皿是*20的大小
char SignTable[]=".!X#"; //符号表
int dish[DishSize][DishSize],res[DishSize][DishSize];
int day,d[16];
int main()
{
int cases; //测试样例数
int i,j,k;
while (cin>>cases)
{
while (cases--)
{
cin>>day; //培养天数
//输入DNA序列信息
for (k=0; k<16; ++k)
cin>>d[k];
//输入培养皿数据
for (i=0; i<DishSize; ++i)
for (j=0; j<DishSize; ++j)
cin>>dish[i][j];
while (day--)
{
for (i=0; i<DishSize; ++i)
for (j=0; j<DishSize; ++j)
{
k = dish[i][j];
//把其本身的值和上下左右的相加
if (i-1>=0)
k += dish[i-1][j];
if (i+1<DishSize)
k += dish[i+1][j];
if (j-1>=0)
k += dish[i][j-1];
if (j+1<DishSize)
k += dish[i][j+1];
res[i][j] = dish[i][j]+d[k];
//不能超过0~3的范围
if (res[i][j]>3)
res[i][j] = 3;
if (res[i][j]<0)
res[i][j] = 0;
}
memcpy (dish,res,sizeof(dish)); //需要头文件#include <string.h>
}
for (i=0; i<DishSize; ++i)
{
for (j=0; j<DishSize; ++j)
cout<<SignTable[dish[i][j]];
cout<<endl;
}
if (cases!=0)
cout<<endl;
}
}
return 0;
}