挑战:编写实现 John H. Conway 的生命游戏细胞自动机的最短程序。[链接]
编辑:经过大约一周的比赛,我选择了一个胜利者:pdehaan ,用 perl 以一个字符击败了 Matlab 解决方案。
对于那些还没有听说过生命游戏的人,您可以使用一个网格(理想情况下是无限的)方形单元格。细胞可以是活的(填充的)或死的(空的)。我们通过应用以下规则确定哪些细胞在下一步中是活着的:
- 任何少于两个活邻居的活细胞都会死亡,好像是由于人口不足造成的。
- 任何有超过三个活邻居的活细胞都会死亡,就好像过度拥挤一样。
- 任何有两三个活邻居的活细胞都可以活到下一代。
- 任何只有三个活邻居的死细胞都会变成活细胞,就像通过繁殖一样。
您的程序将读取指定为命令行参数的 40x80 字符 ASCII 文本文件,以及要执行的迭代次数 (N)。最后,它会将 N 次迭代后的系统状态输出到一个 ASCII 文件 out.txt。
这是一个使用相关文件运行的示例:
在.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(如果存在则覆盖)
- 您不需要处理电路板的边缘(环绕、无限网格等)
- 编辑:您确实需要在输出文件中有换行符。
获胜者将由字符数决定。
祝你好运!