2017年11月04日普及组 Biotech

Description

Bob 发现了一群有趣的细胞。
这群细胞排列成一个 n × m 的矩阵。每个细胞有两个状态:放电状态和平静
状态。它们每秒钟都会按以下的规则转换状态:
首先我们定义,一个细胞的邻居为它周围的 8 个细胞。同时设 k 为某一个细
胞的处于放电状态的邻居的个数。
若 k < 2,则这个细胞在下一秒因电量不足而变为/保持平静状态。
若 k = 2,则这个细胞在下一秒保持原来的状态。
若 k = 3,则这个细胞在下一秒因得到充足的电量而变为/保持放电状态。
若 k > 3,则这个细胞在下一秒因过载而变为/保持平静状态。
Bob 观察了这些细胞现在所处于的状态。他想预测 t 秒后这些细胞的状态。

Input

第一行 3 个正整数 n,m,t。
接下来 n 行,每行一个长度为 m、只包含 01 的字符串,表示每个细胞的初
始状态。’1’ 表示放电状态,’0’ 表示平静状态。

Output

输出 n 行,每行一个长度为 m、只包含 01 的字符串,表示每个细胞的 t 秒
后的状态。’1’ 表示放电状态,’0’ 表示平静状态。

Sample Input

4 4 4
0100
0010
1110
0000
Sample Output

0000
0010
0001
0111
Hint

对于 100% 的数据,1 ≤ n,m ≤ 100,0 ≤ t ≤ 100。

程序:

var
n,m,t,i,j,w,k:longint;
a:array[-1..101,-1..101]of char;
f:array[-1..101,-1..101]of longint;

procedure work;
var
i,j:longint;
begin
    for i:=1 to n do
    for j:=1 to m do
    if f[i,j]=1 then a[i,j]:='1' else a[i,j]:='0';
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 w:=1 to t do
    begin
        fillchar(f,sizeof(f),0);
        for i:=1 to n do
        for j:=1 to m do
        begin
            k:=0;
            if a[i-1,j-1]='1' then inc(k);
            if a[i-1,j]='1' then inc(k);
            if a[i-1,j+1]='1' then inc(k);
            if a[i,j-1]='1' then inc(k);
            if a[i,j+1]='1' then inc(k);
            if a[i+1,j-1]='1' then inc(k);
            if a[i+1,j]='1' then inc(k);
            if a[i+1,j+1]='1' then inc(k);

            if k<2 then f[i,j]:=0 else
            if k=2 then f[i,j]:=ord(a[i,j])-ord('0') else
            if k=3 then f[i,j]:=1 else
            if k>3 then f[i,j]:=0;
        end;
        work;
    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.
原文地址:https://www.cnblogs.com/YYC-0304/p/9500027.html