HDOJ 1057 A New Growth Industry
题目
分类
模拟
题意
模拟细胞生长
用 20 * 20 的方格模拟 培养皿
规则由 16个整形的数组 D 定义
每个单元 有 0 - 3 三种形态 如图
![]()
当前单元数字 加上 上下左右单元数字 之和 为 sum
单元当前数字 + D[sum] > 3 则 为3 如果 < 0 则为 0 否则 为 其
给出当当前培养皿 求 n次后的培养皿 数字 按上图所示输出
题解
按题意模拟即可
代码
#include <iostream>
#include <cstring>
#define max 20
char crt[4] = {'.','!','X','#'};
int cb[max][max];
int ca[max][max];
int d[max];
using namespace std;
int output(int cur[][max]);
int simulation(int before[][max],int after[][max]);
int main()
{
int n,m;
cin >> n;
while(n--)
{
memset(cb,0,sizeof(cb));
memset(ca,0,sizeof(ca));
cin >> m;
for(int i = 0;i < 16;i++)
cin >> d[i];
for(int i = 0;i < max;i++)
for(int j = 0;j < max;j++)
cin >> cb[i][j];
for(int i = 0;i < m;i++)
{
if(i % 2 == 0)
simulation(cb,ca);
else
simulation(ca,cb);
}
if(m % 2 == 0)
output(cb);
else
output(ca);
if(n > 0)
cout << endl;
}
return 0;
}
int output(int cur[][max])
{
for(int i = 0;i < max;i++)
{
for(int j = 0;j < max;j++)
{
switch(cur[i][j])
{
case 0 : cout << crt[0]; break;
case 1 : cout << crt[1]; break;
case 2 : cout << crt[2]; break;
case 3 : cout << crt[3]; break;
}
}
cout << endl;
}
return 0;
}
int simulation(int before[][max],int after[][max])
{
int sum,rs;
for(int i = 0;i < max;i++)
for(int j = 0;j < max;j++)
{
sum = 0;
sum += (i > 0 ? before[i-1][j] : 0);
sum += (j > 0 ? before[i][j-1] : 0);
sum += (i < (max-1) ? before[i+1][j] : 0);
sum += (j < (max-1) ? before[i][j+1] : 0);
sum += before[i][j];
rs = before[i][j] + d[sum];
if(rs > 3)
rs = 3;
if(rs < 0)
rs = 0;
after[i][j] = rs;
}
return 0;
}