`

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

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组成,0代表死亡的元素,1代表活的元素。数组是变化的,变化的规则有四个,当一个活的元素周围有两个或三个活的元素时,下一个阶段它会存活;如果它的周围活的元素小于两个或者多于三个下一个阶段它都将死亡。对于一个死亡的元素,如果它的周围有三个活的元素,下一个阶段它就会复活。

我们一次判断数组中的元素,得到它周围有几个活的元素,根据规则判断它下一个阶段是否存活,如果存活就进行标记,一次处理完所有的元素。最后将标记的元素变为1,其余为0即可。代码如下:
public class Solution {
    public void gameOfLife(int[][] board) {
        if(board == null || board.length == 0) return;
        int m = board.length;
        int n = board[0].length;
        for(int i = 0; i < m; i++) {
            for(int j = 0; j < n; j++) {
                int countLive = getLive(i, j, m, n, board);
                if(board[i][j] == 1) {
                    if(countLive == 2 || countLive == 3)
                        board[i][j] += 10;
                } else {
                    if(countLive == 3)
                        board[i][j] += 10;
                }
            }
        }
        for(int i = 0; i < m; i++) {
            for(int j = 0; j < n; j++) {
                if(board[i][j] / 10 == 1) {
                    board[i][j] = 1;
                } else {
                    board[i][j] = 0;
                }
            }
        }
    }
    private int getLive(int x, int y, int m, int n, int[][] board) {
        int count = 0;
        for(int i = x - 1; i <= x + 1; i ++) {
            for(int j = y - 1; j <= y + 1; j ++) {
                if(i < 0 || j < 0 || i == m || j == n || (i == x && j == y)) continue;
                if(board[i][j] % 10 == 1) count ++;
            }
        }
        return count;
    }
}
0
3
分享到:
评论

相关推荐

    the game of life

    the game of life,C++,亲测可以运行。Main.cpp #include "utility.h" #include "life.h" void main() // Program to play Conway's game of Life. /* Pre: The user supplies an initial configuration of living ...

    game of life 测试数据生成

    《生命游戏》(Game of Life)是英国数学家约翰·康威提出的一种细胞自动机,它以简单的规则模拟复杂的生命现象。在这个系统中,每个细胞都有两种状态:活或者死。细胞的状态会在每一代根据其周围邻居的状态进行更新...

    game of life 代码简单 风格经典 适合新手

    《生命游戏》(Game of Life)是英国数学家约翰·康威提出的一种细胞自动机,它以简单的规则展示了复杂的行为,是计算机科学和数学领域的经典示例。这个标题表明我们将探讨一个适合新手学习的《生命游戏》代码实现,...

    Game Of Life

    《生命游戏》(Game Of Life)是英国数学家约翰·康威提出的一种细胞自动机,它以简单的规则展示了复杂的行为,被誉为“最简单的宇宙模型”。在这个“游戏”中,细胞在二维网格上按照预设的规则演化,这些规则基于...

    python实现生命游戏的示例代码(Game of Life)

    ### Python 实现的生命游戏(Game of Life) #### 一、引言 生命游戏(Game of Life)是一种零玩家的策略游戏,由英国数学家约翰·何顿·康威于1970年发明。游戏规则简单但演化过程复杂,能够产生各种各样的模式,...

    game_of_life.zip_game of life_game of life java_game_of_life_lif

    《生命游戏》(Game of Life)是由英国数学家约翰·康威在1970年提出的细胞自动机,它是一个简单的模拟系统,展现了复杂行为和动态模式的产生。在这个游戏中,虚拟的细胞在二维网格上根据一些简单的规则进行演化。这...

    The_game_of_Life.rar_THE GAME OF LIFE_game of life

    《生命游戏》(The Game of Life),又称为“康威生命游戏”,是由数学家约翰·何顿·康威(John Horton Conway)在1970年提出的细胞自动机理论的一个著名例子。这个游戏并非传统意义上的“游戏”,而是一个展示复杂...

    Game_of_life_rand.rar_game of life_game of life matlab_matlab ga

    生命游戏,又称Conway's Game of Life,是由数学家约翰·何顿·康威在1970年提出的一种简单的细胞自动机模型。这个模型基于一套简单的规则,却能展现出复杂的动态行为,模拟出类似生物演化、群体行为等现象。在...

    (源码)基于CC++(PokittoLib)的Game of Life与Space Escape游戏项目.zip

    # 基于CC++(PokittoLib)的Game of Life与Space Escape游戏项目 ## 项目简介 本项目围绕Pokitto游戏控制台开发了两款游戏,分别是Game of Life与Space Escape。Game of Life模拟生命的演化,基于康威的生命游戏...

    (源码)基于C语言的Game of Life模拟项目.zip

    # 基于C语言的Game of Life模拟项目 ## 一、项目简介 这是一个基于C语言的Game of Life模拟项目,旨在使用控制台应用程序模拟生命游戏的运行过程。该项目使用传统的串行编程方式,并提供了使用OpenMP和CUDA进行...

    (源码)基于凹语言的Conway's Game of Life模拟器.zip

    # 基于凹语言的Conway's Game of Life模拟器 ## 项目简介 本项目是一个基于凹语言的Conway's Game of Life(生命游戏)模拟器。生命游戏是一个经典的细胞自动机模型,模拟了一个二维栅格世界中的生命演化过程。每...

    3D_game of life_3d元胞自动机_三维生命游戏_gameoflife_生命游戏

    在计算机科学和数学的世界里,有一个名为“生命游戏”(Game of Life)的经典模型,它是由英国数学家约翰·康威(John Horton Conway)在1970年提出的。这个简单的规则系统,通过二维网格上的细胞状态变化,揭示出...

    TheGameOfLife

    模拟细胞的生命游戏,通过编写类来模拟细胞的整体,然后输出来描述细胞的存活状态,其中细胞更细换代满足一定的规则

    生命游戏 源码 vc Game of life

    《生命游戏》(Game of Life)是英国数学家约翰·康威提出的一种细胞自动机模型,它通过简单的规则模拟复杂的生命现象。在这个源码中,我们看到的是使用Visual C++(VC)编写的版本,这使得程序员能够用C++语言来...

    game of life

    生命游戏的串行实现c语言基础上的实现,有界面

    GameOfLife_java.rar_game of life_game of life ja_元胞自动机_生命_生命游戏

    《生命游戏》(Game of Life)是由英国数学家约翰·康威在1970年提出的一种简单的模拟生命过程的模型,它是一种元胞自动机的典型应用。元胞自动机是一种离散的时间和空间的计算模型,由一维、二维或更高维度的格子...

    game of life的x10文件

    著名的game of life的x10代码,算法请查找维基百科

    java-leetcode题解之Game of Life.java

    本次我们聚焦于Java语言在LeetCode平台上的一个经典算法题目——“Game of Life”。这个题目是一个关于细胞自动机的算法实现,要求编写一个函数,根据给定的规则更新二维矩阵中每个细胞的状态,实现生命游戏的进化...

    C# 控制台 Console Conway's game of life 康威 生命游戏 源码

    康威生命游戏(Conway's game of life) 游戏以细胞为单元,每个细胞有两种状态:存活、死亡。 游戏有下面几条规则: 当细胞存活时,如果周围八格存活细胞时(不包含2个),该细胞死亡。 当细胞存活时,如果周围...

Global site tag (gtag.js) - Google Analytics