资讯专栏INFORMATION COLUMN

[LeetCode] 289. Game of Life

Ajian / 1591人阅读

Problem

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. The next state is created by applying the above rules simultaneously to every cell in the current state, where births and deaths occur simultaneously.

Example:

Input:

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

Output:

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

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?

Solution
class Solution {
    public void gameOfLife(int[][] board) {
        if (board == null || board.length == 0 || board[0].length == 0) return;
        
        //how to get to neighbors for board[i][j] -> use int[8][2] to store offset in 8 directions
        int[][] directions = {{-1,-1},{-1,0},{-1,1},{0,1},{0,-1},{1,-1},{1,0},{1,1}};
        for (int i = 0; i < board.length; i++) {
            for (int j = 0; j < board[0].length; j++) {
                int lives = 0;
                for (int k = 0; k < directions.length; k++) {
                    int x = i+directions[k][0];
                    int y = j+directions[k][1];
                    if (x >= 0 && x < board.length && 
                        y >= 0 && y < board[0].length && 
                        board[x][y]%2 == 1) lives++;
                }
                
                if (board[i][j]%2 == 1) {
                    if (lives == 2 || lives == 3) board[i][j] += 2;
                } else {
                    if (lives == 3) board[i][j] += 2;
                }
            }
        }
        
        //after +2 operations for all live cells, the dying cells still hold 1 or 0
        for (int i = 0; i < board.length; i++) {
            for (int j = 0; j < board[0].length; j++) {
                //eliminate 1s and 0s to 0, 3s and 2s to 1
                board[i][j] >>= 1;
            }
        }
    }
}

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

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

相关文章

  • leetcode289. Game of Life

    摘要:板上的每个小格子有两种状态,或。而根据游戏规则,每一次这个板上的内容将会随着前一次板上的内容发生变化。然后再根据当前格子的状态计算当前格子的下一个状态。当然最后别忘了将原始状态传递出去。 题目要求 According to the Wikipedias article: The Game of Life, also known simply as Life, is a cellular...

    jerryloveemily 评论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元查看
<