- 浏览: 185424 次
- 性别:
- 来自: 济南
文章分类
最新评论
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 271Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 274You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 392Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 380Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 506Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 570Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 484Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 674Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 477The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 436Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 585Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 594Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 431All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 908Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 936Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 608Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 700Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 866Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 794You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 732For 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年提出的一种简单的细胞自动机模型。这个模型基于一套简单的规则,却能展现出复杂的动态行为,模拟出类似生物演化、群体行为等现象。在...
模拟细胞的生命游戏,通过编写类来模拟细胞的整体,然后输出来描述细胞的存活状态,其中细胞更细换代满足一定的规则
《生命游戏》(Game of Life)是英国数学家约翰·康威提出的一种细胞自动机模型,它通过简单的规则模拟复杂的生命现象。在这个源码中,我们看到的是使用Visual C++(VC)编写的版本,这使得程序员能够用C++语言来...
生命游戏的串行实现c语言基础上的实现,有界面
《生命游戏》(Game of Life)是由英国数学家约翰·康威在1970年提出的一种简单的模拟生命过程的模型,它是一种元胞自动机的典型应用。元胞自动机是一种离散的时间和空间的计算模型,由一维、二维或更高维度的格子...
著名的game of life的x10代码,算法请查找维基百科
康威生命游戏(Conway's game of life) 游戏以细胞为单元,每个细胞有两种状态:存活、死亡。 游戏有下面几条规则: 当细胞存活时,如果周围八格存活细胞时(不包含2个),该细胞死亡。 当细胞存活时,如果周围...
《生命游戏》(Game of Life),由英国数学家约翰·康威在1970年提出,是一种简单的模拟生命过程的计算机程序。这个程序基于一套简单的规则,却能展现出复杂而多样的行为,因此它成为了元胞自动机的代表作,并在...
生命游戏,全称为“Game Of Life”,是由英国数学家约翰·康威(John Horton Conway)在1970年提出的一种简单的模拟生物演化的计算模型,它是细胞自动机的一个经典实例。细胞自动机是一种在离散空间和离散时间上的...
康威生命游戏(Conway's Game of Life)是由数学家约翰·何顿·康威(John Horton Conway)在1970年提出的一种细胞自动机,它是一个在二维网格上模拟简单生命规则的数学模型。这个模型通过一些基本的、局部的交互...
一种在很多格子里不断进化的生命进化的小游戏。 它的规则是这样的: 对其中一个网格,如果它的邻居少于两个,则它会死于孤独;如果多于三个,它亦会因拥挤而死。只有当它的邻居数等于二或三时它才会生存到下一代;...
在本资源包“python 零基础学习篇-08Quiz oo、game of life.zip”中,包含了Python编程初学者的两个重要主题:面向对象编程(Object-Oriented Programming, OOP)和“生命游戏”(Game of Life)。让我们深入探讨这...