资讯专栏INFORMATION COLUMN

leetcode289. Game of Life

jerryloveemily / 1997人阅读

摘要:板上的每个小格子有两种状态,或。而根据游戏规则,每一次这个板上的内容将会随着前一次板上的内容发生变化。然后再根据当前格子的状态计算当前格子的下一个状态。当然最后别忘了将原始状态传递出去。

题目要求
According to the Wikipedia"s article: "The Game of Life, also known simply as Life, is a cellular automaton devised by the British mathematician John Horton Conway in 1970."

Given a board with m by n cells, each cell has an initial state live (1) or dead (0). Each cell interacts with its eight 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?

这个内容在维基百科上讲述的非常详细,有图文示例。
这个游戏玩家在游戏开始后是不能操作的,完全是由最初的状态来决定最终的结果。板上的每个小格子有两种状态,livedead
而根据游戏规则,每一次这个板上的内容将会随着前一次板上的内容发生变化。变化的规则如下:

如果当前格子为live,那么只要它周围live邻居的数量大于3个或是小于2个,该格子就会变成dead状态。

如果当前个字为dead,那么只要它周围live的邻居数量正好为3个,该格子就会变成live状态。否则还是dead状态。

现在输入一个状态,让我们更新出板子的下一个状态。

思路和代码

这里其实用暴力查询的话也是可以解决的。也就是每遇到一个格子就将该格子的邻居统统遍历一下,计算一个邻居里面live和dead的数量。然后再根据当前格子的状态计算当前格子的下一个状态。但是这里需要一个新的m*n的数组来保存新的状态,有点浪费空间。而且不能很好的利用之前的遍历结果。所以我们可以将之前的遍历结果用某种方式直接存到当前的格子里,减少一些遍历次数。

假设现在有这样的一个board:

[
 [1,0,1],
 [0,0,1],
 [1,1,0],
]

我们从左上角开始遍历,我们发现它的周围一个live的邻居都没有,因此它的状态将会变为0。但是与此同时,它的上一个状态为1,需要传递给周围的邻居,因此我们可以采用将周围邻居+10的方式传递。如下:

[
 [0,10,1],
 [10,10,1],
 [1,1,0],
]

这是我们再看坐标为(0,1)的格子。假设当前个字上的值为cur,那么cur/10则是之前邻居的数量,cur%10则代表该格子的初始状态。根据这个规律,我们可以知道当前格子上的初始状态为0即dead,而之前遍历过的邻居有1个。这时我们就无需遍历所有的邻居,只需要继续访问还未访问的邻居即可。当然最后别忘了将原始状态传递出去。

代码如下:

    public void gameOfLife(int[][] board) {
       if(board==null || board.length==0) return;
       int row = board.length;
       int column = board[0].length;
       int multiply = 10;
       for(int i = 0 ; i=0 && isLive(board[i+1][j-1])){liveNeighboars++;}
                   if(j+13){
                       board[i][j] = 0;
                   }else{
                       board[i][j] = 1;
                   }
                   if(j+1=0 && i+1


想要了解更多开发技术,面试教程以及互联网公司内推,欢迎关注我的微信公众号!将会不定期的发放福利哦~

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/68653.html

相关文章

  • [LeetCode] 289. Game of Life

    Problem According to the Wikipedias article: The Game of Life, also known simply as Life, is a cellular automaton devised by the British mathematician John Horton Conway in 1970. Given a board with m ...

    Ajian 评论0 收藏0
  • 289. Life of Game

    摘要:问题解答我的解法是需要最多的空间的。如果要做到那么我看到里一个非常的做法是用一个的数表示改变前和改变后的状态。 问题:According to the Wikipedias article: The Game of Life, also known simply as Life, is a cellular automaton devised by the British mathem...

    chuyao 评论0 收藏0
  • [Leetcode] Game of Life 生命游戏

    摘要:思路普通解法,遍历每一个细胞求值,用一个的矩阵存放结果。求值过程,稍微分析一下可知,其实就是按照以下的矩阵进行结果是可数的。 According to the Wikipedias article: The Game of Life, also knownsimply as Life, is a cellular automaton devised by the Britishmath...

    android_c 评论0 收藏0
  • [Leetcode] Game of Life 生命游戏

    摘要:如果多核的机器如何优化因为是多核,我们可以用线程来实现并行计算。如果线程变多分块变多,边缘信息也会变多,开销会增大。所以选取线程的数量是这个开销和并行计算能力的折衷。 Game of Life I According to the Wikipedias article: The Game of Life, also known simply as Life, is a cellula...

    XFLY 评论0 收藏0
  • Python+Pygame实操之玩命吃水果游戏的完成

      吃豆人和削苹果这两个游戏想必大家都知道吧,本文运用Python里的Pygame控制模块编写出一个融合吃豆人+切水果的新手游:玩命吃苹果,有兴趣的话可以认识一下  引言  哈哈哈!木木子今天浮现——早已来给大家看了不少具体内容啦~  涉及到的人工智能、新手、网络爬虫、数据统计分析(这一块的通常但是审批)手机游戏...  PS:  吃豆人我写过了哈  Python+Pygame实战之吃豆豆游戏的实...

    89542767 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<