挑战:编写实现John H. Conway的Game of Life细胞自动机的最短程序。[链接]
编辑:经过对竞争的一个星期,我选择了一个胜利者:pdehaan,管理由击败了Matlab解决一个字符用Perl。
对于那些还没有听说过“生命游戏”的人,您可以选择一个正方形的网格(理想情况下是无限个)。细胞可以是活的(填充的)或死亡的(空的)。通过应用以下规则,我们可以确定在下一个步骤中哪些细胞仍然存在:
- 任何具有少于两个活邻居的活细胞都会死亡,好像是由于人口不足造成的。
- 任何具有三个以上活邻居的活细胞都会死亡,就像人满为患一样。
- 任何有两个或三个活邻居的活细胞都可以存活到下一代。
- 具有正好三个活邻居的任何死细胞都将变成活细胞,就像通过繁殖一样。
您的程序将读取指定为命令行参数的40x80字符的ASCII文本文件,以及要执行的迭代次数(N)。最后,它将在N次迭代后将系统状态输出到ASCII文件out.txt。
这是运行相关文件的示例:
in.txt:
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
..................................XX............................................
..................................X.............................................
.......................................X........................................
................................XXXXXX.X........................................
................................X...............................................
.................................XX.XX...XX.....................................
..................................X.X....X.X....................................
..................................X.X......X....................................
...................................X.......XX...................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
重复100次:
Q:\>life in.txt 100
结果输出(out.txt)
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
..................................XX............................................
..................................X.X...........................................
....................................X...........................................
................................XXXXX.XX........................................
................................X.....X.........................................
.................................XX.XX...XX.....................................
..................................X.X....X.X....................................
..................................X.X......X....................................
...................................X.......XX...................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
规则:
- 您需要使用文件I / O来读取/写入文件。
- 您需要接受一个输入文件,并将迭代次数作为参数
- 您需要以指定格式生成out.txt(如果存在则覆盖)
- 你并不需要处理板的边缘(环绕,无限电网.etc)
- 编辑:您确实需要在输出文件中包含换行符。
获胜者将由字符数决定。
祝好运!