Leetcode 289 Game of Life

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):

  1. Any live cell with fewer than two live neighbors dies, as if caused by under-population.
  2. Any live cell with two or three live neighbors lives on to the next generation.
  3. Any live cell with more than three live neighbors dies, as if by over-population..
  4. 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:

  1. 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.
  2. 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?

思路:要求 in-place ,所以要区分三种变化状态 1->0,1->1,0->1,将三种状态分别用-1/-2/-3三个状态值来表示

  第一次遍历时,先判断当前cell的转换状态,且将cell值置为对应状态值,在后面的遍历中根据状态值则可推断出原始值,进而判断转换状态。

  第二次遍历,则将状态值置为变换后的值

public class S289 {
    public void gameOfLife(int[][] board) {
        int m = board.length,n = board[0].length;
        for (int i = 0;i < m;i++) {
            for (int j = 0;j < n;j++) {
                int liveCount = 0;
                for (int k = i-1;k <= i+1;k++) {
                    for (int l = j-1;l <= j+1;l++) {
                        if(k < 0 || l < 0 || k > m-1 || l > n-1 || (k == i && l == j)) continue;
                        liveCount += getRealNum(board[k][l]);
                    }
                }
                if (board[i][j] == 1) {
                    if (liveCount <2 || liveCount >3 ) {
                        board[i][j] = -1;
                    } else {
                        board[i][j] = -2;
                    }
                } else {
                    if (liveCount == 3) {
                        board[i][j] = -3;
                    }
                }
            }
        }
        for (int i = 0;i < m;i++) {
            for (int j = 0;j < n;j++) {
                if (board[i][j] == -2 || board[i][j] == -3)
                    board[i][j] = 1;
                else if (board[i][j] == -1)
                    board[i][j] = 0;
            }
        }
    }
    public static int getRealNum(int i){
        if(i == -1 || i == -2)
            return 1;
        else if (i == -3) {
            return 0;
        }
        return i;
    }
    public static void main(String[] args) {
        S289 s = new S289();
        int [][]b = {{1}};
        s.gameOfLife(b);
    }
}
时间: 2024-08-15 07:14:57

Leetcode 289 Game of Life的相关文章

LeetCode 289 - 生命游戏 - 原地算法

Wikipedia关于原地算法的描述:原地算法(in-place algorithm)基本上不需要额外辅助的数据结构,然而,允许少量额外的辅助变量来转换数据的算法.当算法运行时,输入的数据通常会被要输出的部分覆盖掉. 很容易看出,原地算法的特点是不需要辅助的数据结构而只需要辅助变量.通常,维护一个复杂的数据结构时间空间复杂度都是比较高的,原地算法的优越性正来自于不依赖数据结构,尽管它借鉴了数据结构的思想. 同时也不难看出,原地算法总体的实现思路就是用输出的数据在原地覆盖已经“没用”的输入的数据.

[leetcode] 289. Game of Life 解题报告

题目链接: https://leetcode.com/problems/game-of-life/ 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 

LeetCode 289. Game of Life (C++)

题目: 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 liv

Leetcode 289.生命游戏

生命游戏 根据百度百科,生命游戏,简称为生命,是英国数学家约翰·何顿·康威在1970年发明的细胞自动机. 给定一个包含 m × n 个格子的面板,每一个格子都可以看成是一个细胞.每个细胞具有一个初始状态 live(1)即为活细胞, 或 dead(0)即为死细胞.每个细胞与其八个相邻位置(水平,垂直,对角线)的细胞都遵循以下四条生存定律: 如果活细胞周围八个位置的活细胞数少于两个,则该位置活细胞死亡: 如果活细胞周围八个位置有两个或三个活细胞,则该位置活细胞仍然存活: 如果活细胞周围八个位置有超过

[email&#160;protected] [289] Game of Life

https://leetcode.com/problems/game-of-life/ 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 c

LeetCode:Game of Life - 康威生命游戏

1.题目名称 Game of Life(康威生命游戏) 2.题目地址 https://leetcode.com/problems/game-of-life 3.题目内容 英文: 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 Conw

【leetcode】289. Game of Life

题目如下: 解题思路:因为本题要求的是直接在输入的数组上面修改,而且细胞的生死转换是一瞬间的事情,所以需要引入两个中间状态,待死和待活.这两个中间状态用于在四条规则判断的时候是"活"和"死",但是呈现在最终的结果是"死"和"活". 代码如下: class Solution(object): def getLiveCount(self,board,x,y): l = [(0,-1),(0,1),(-1,0),(1,0),(-1,

[Leetcode] 第289题 生命游戏

一.题目描述 根据百度百科,生命游戏,简称为生命,是英国数学家约翰·何顿·康威在1970年发明的细胞自动机. 给定一个包含 m × n 个格子的面板,每一个格子都可以看成是一个细胞.每个细胞具有一个初始状态 live(1)即为活细胞, 或 dead(0)即为死细胞.每个细胞与其八个相邻位置(水平,垂直,对角线)的细胞都遵循以下四条生存定律: 如果活细胞周围八个位置的活细胞数少于两个,则该位置活细胞死亡: 如果活细胞周围八个位置有两个或三个活细胞,则该位置活细胞仍然存活: 如果活细胞周围八个位置有

73. Set Matrix Zeroes &amp;&amp; 289. Game of Life

73. Set Matrix Zeroes Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. Hide Tags Array Hide Similar Problems (M) Game of Life public class Solution { //Only consider the zeros that exist originally. public