频道栏目
首页 > 资讯 > 其他 > 正文

解决Game of Life问题方法

17-10-31        来源:[db:作者]  
收藏   我要投稿

问题描述:

According to theWikipedia's article: "TheGame of Life, also known simply asLife, is a cellular automaton devised by the British mathematician John Horton Conway in 1970."

Given aboardwithmbyncells, each cell has an initial statelive(1) ordead(0). Each cell interacts with itseight neighbors(horizontal, vertical, diagonal) using the following four rules (taken from the above Wikipedia article):

Any live cell with fewer than two live neighbors dies, as if caused by under-population.

Any live cell with two or three live neighbors lives on to the next generation.

Any live cell with more than three live neighbors dies, as if by over-population..

Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.

Write a function to compute the next state (after one update) of the board given its current state.

Follow up:

Could you solve it in-place? Remember that the board needs to be updated at the same time: You cannot update some cells first and then use their updated values to update other cells.

In this question, we represent the board using a 2D array. In principle, the board is infinite, which would cause problems when the active area encroaches the border of the array. How would you address these problems?

问题分析:

在原有空间内改变同时所有的状态,我么可以利用位操作,我们发现所有的状态都是由0和1构成的,那么我们可以把下一个状态存储到第二位,到时直接向右移位即可。

过程详见代码:

class Solution {

public:

void gameOfLife(vector>& board) {

int m = board.size(), n = m ? board[0].size() : 0;

for (int i = 0; i < m; i++)

{

for (int j = 0; j < n; j++)

{

int count = 0;

for (int I = max(i - 1, 0); I>= 1;

}

}

};

 

相关TAG标签
上一篇:c编译器的实现,利用flex实现字符串的识别,利用bison实现ast语法树的构建
下一篇:计算分数“编程题”
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站