- 浏览: 188368 次
- 性别:
- 来自: 济南
-
文章分类
最新评论
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即可。代码如下:
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; } }
发表评论
-
498. Diagonal Traverse
2019-11-15 13:52 288Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 292You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 413Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 395Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 521Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 600Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 503Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 696Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 495The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 450Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 611Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 625Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 451All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 924Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 950Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 626Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 715Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 896Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 809You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 749For a undirected graph with tre ...
相关推荐
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)是英国数学家约翰·康威提出的一种细胞自动机,它以简单的规则展示了复杂的行为,被誉为“最简单的宇宙模型”。在这个“游戏”中,细胞在二维网格上按照预设的规则演化,这些规则基于...
### Python 实现的生命游戏(Game of Life) #### 一、引言 生命游戏(Game of Life)是一种零玩家的策略游戏,由英国数学家约翰·何顿·康威于1970年发明。游戏规则简单但演化过程复杂,能够产生各种各样的模式,...
《生命游戏》(Game of Life)是由英国数学家约翰·康威在1970年提出的细胞自动机,它是一个简单的模拟系统,展现了复杂行为和动态模式的产生。在这个游戏中,虚拟的细胞在二维网格上根据一些简单的规则进行演化。这...
《生命游戏》(The Game of Life),又称为“康威生命游戏”,是由数学家约翰·何顿·康威(John Horton Conway)在1970年提出的细胞自动机理论的一个著名例子。这个游戏并非传统意义上的“游戏”,而是一个展示复杂...
生命游戏,又称Conway's Game of Life,是由数学家约翰·何顿·康威在1970年提出的一种简单的细胞自动机模型。这个模型基于一套简单的规则,却能展现出复杂的动态行为,模拟出类似生物演化、群体行为等现象。在...
# 基于CC++(PokittoLib)的Game of Life与Space Escape游戏项目 ## 项目简介 本项目围绕Pokitto游戏控制台开发了两款游戏,分别是Game of Life与Space Escape。Game of Life模拟生命的演化,基于康威的生命游戏...
# 基于C语言的Game of Life模拟项目 ## 一、项目简介 这是一个基于C语言的Game of Life模拟项目,旨在使用控制台应用程序模拟生命游戏的运行过程。该项目使用传统的串行编程方式,并提供了使用OpenMP和CUDA进行...
# 基于凹语言的Conway's Game of Life模拟器 ## 项目简介 本项目是一个基于凹语言的Conway's Game of Life(生命游戏)模拟器。生命游戏是一个经典的细胞自动机模型,模拟了一个二维栅格世界中的生命演化过程。每...
在计算机科学和数学的世界里,有一个名为“生命游戏”(Game of Life)的经典模型,它是由英国数学家约翰·康威(John Horton Conway)在1970年提出的。这个简单的规则系统,通过二维网格上的细胞状态变化,揭示出...
模拟细胞的生命游戏,通过编写类来模拟细胞的整体,然后输出来描述细胞的存活状态,其中细胞更细换代满足一定的规则
《生命游戏》(Game of Life)是英国数学家约翰·康威提出的一种细胞自动机模型,它通过简单的规则模拟复杂的生命现象。在这个源码中,我们看到的是使用Visual C++(VC)编写的版本,这使得程序员能够用C++语言来...
生命游戏的串行实现c语言基础上的实现,有界面
《生命游戏》(Game of Life)是由英国数学家约翰·康威在1970年提出的一种简单的模拟生命过程的模型,它是一种元胞自动机的典型应用。元胞自动机是一种离散的时间和空间的计算模型,由一维、二维或更高维度的格子...
著名的game of life的x10代码,算法请查找维基百科
本次我们聚焦于Java语言在LeetCode平台上的一个经典算法题目——“Game of Life”。这个题目是一个关于细胞自动机的算法实现,要求编写一个函数,根据给定的规则更新二维矩阵中每个细胞的状态,实现生命游戏的进化...
康威生命游戏(Conway's game of life) 游戏以细胞为单元,每个细胞有两种状态:存活、死亡。 游戏有下面几条规则: 当细胞存活时,如果周围八格存活细胞时(不包含2个),该细胞死亡。 当细胞存活时,如果周围...