2 Biotech
1s; 256M
2.1 题目描述
Bob 发现了一群有趣的细胞。
这群细胞排列成一个n m 的矩阵。每个细胞有两个状态:放电状态和平静
状态。它们每秒钟都会按以下的规则转换状态:
首先我们定义,一个细胞的邻居为它周围的8 个细胞。同时设k 为某一个细
胞的处于放电状态的邻居的个数。
若k < 2,则这个细胞在下一秒因电量不足而变为/保持平静状态。
若k = 2,则这个细胞在下一秒保持原来的状态。
若k = 3,则这个细胞在下一秒因得到充足的电量而变为/保持放电状态。
若k > 3,则这个细胞在下一秒因过载而变为/保持平静状态。
Bob 观察了这些细胞现在所处于的状态。他想预测t 秒后这些细胞的状态。
2.2 输入格式
第一行3 个正整数n; m; t。
接下来n 行,每行一个长度为m、只包含01 的字符串,表示每个细胞的初
始状态。’1’ 表示放电状态,’0’ 表示平静状态。
2.3 输出格式
输出n 行,每行一个长度为m、只包含01 的字符串,表示每个细胞的t 秒
后的状态。’1’ 表示放电状态,’0’ 表示平静状态。
分析:统计出每个细胞邻居放电个数,然后再改变状态。
代码
const
maxn=1000;
dx:array[1..8] of longint=(-1,-1,0,1,1,1,0,-1);
dy:array[1..8] of longint=(0,1,1,1,0,-1,-1,-1);
var
a:array[0..maxn,0..maxn] of char;
b:array[0..maxn,0..maxn] of longint;
n,m,t,i,j,k,l,x,y:longint;
function check(x,y:longint):boolean;
begin
check:=true;
if (x<1) or (y<1) or (x>n) or (y>m) then exit(false);
end;
begin
//assign(input,'biotech.in');reset(input);
//assign(output,'biotech.out');rewrite(output);
readln(n,m,t);
for i:=1 to n do
begin
for j:=1 to m do
read(a[i,j]);
readln;
end;
for l:=1 to t do
begin
fillchar(b,sizeof(b),0);
for i:=1 to n do
for j:=1 to m do
for k:=1 to 8 do
begin
x:=i+dx[k];
y:=j+dy[k];
if check(x,y) then
if a[x,y]='1' then inc(b[i,j]);
end;
for i:=1 to n do
for j:=1 to m do
begin
if b[i,j]<2 then if a[i,j]='1' then a[i,j]:='0';
if b[i,j]=3 then if a[i,j]='0' then a[i,j]:='1';
if b[i,j]>3 then if a[i,j]='1' then a[i,j]:='0';
end;
end;
for i:=1 to n do
begin
for j:=1 to m do
write(a[i,j]);
writeln;
end;
close(input);close(output);
end.