编码操练招式——康威生命游戏

来源:互联网 发布:考勤软件叫什么 编辑:程序博客网 时间:2024/05/17 01:37
康威生命游戏(Conway’s Game Of Life)


在一个有边界的二维网格(Grid)中,每个格子中生活着一个细胞(Cell);每个细胞只有两种生命状态:生(Alive)或死(Dead);网格矩阵边界之外没有细胞;
每天天一亮,所有格子里的细胞都统一通过下面4个规则,进化到下一代。每个细胞下一代的生命状态与自己和8个邻居进化前的生命状态有关:
1)对于活的细胞,若其活着的邻居过于稀少,以至于少于2个,那么它的下一代就死去了;
2)对于活的细胞,若其活着的邻居过于稠密,以至于多于3个,那么它的下一代也会死去;
3)对于活的细胞,若其活着的邻居恰好是2~3个,那么它的下一代会接着愉快地活着;
4)对于死的细胞,若其活着的邻居恰好是3个,那么它的下一代会复生。


Your task is to write a program to calculate the next generation of Conway's game of life, given any starting position. You start with a two dimensional grid of cells, where each cell is either alive or dead. The grid is finite, and no life can exist off the edges. When calculating the next generation of the grid, follow these four rules:

1. Any live cell with fewer than two live neighbours dies, as if caused by underpopulation.
2. Any live cell with more than three live neighbours dies, as if by overcrowding.
3. Any live cell with two or three live neighbours lives on to the next generation.
4. Any dead cell with exactly three live neighbours becomes a live cell.


Examples: * indicates live cell, . indicates dead cell


Example input: (4 x 8 grid)
4 8
........
....*...
...**...
........


Example output:
4 8
........
...**...
...**...
........


(英文资料来源:http://www.cyber-dojo.com/setup/show)






原创粉丝点击