- 浏览: 174717 次
- 性别:
- 来自: 济南
文章分类
最新评论
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 229Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 232You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 348Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 342Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 461Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 525Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 435Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 624Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 435The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 392Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 522Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 542Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 379All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 870Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 885Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 560Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 612Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 771Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 741You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 635For 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 Metro Style App
主要介绍了python实现生命游戏的示例代码(Game of Life),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
[game of life] in complexed system
*************************** ****新手上路**************** **************************** 代码简单 风格经典 适合新手
模拟细胞的生命游戏,通过编写类来模拟细胞的整体,然后输出来描述细胞的存活状态,其中细胞更细换代满足一定的规则
生命游戏的串行实现c语言基础上的实现,有界面
经典的game of life
生命游戏 源码 vc Game of life beautiful and interesting 敲空格开始游戏 knock at space bar to start the game
著名的game of life的x10代码,算法请查找维基百科
康威生命游戏(Conway's game of life) 游戏以细胞为单元,每个细胞有两种状态:存活、死亡。 游戏有下面几条规则: 当细胞存活时,如果周围八格存活细胞时(不包含2个),该细胞死亡。 当细胞存活时,如果周围...
Conway's Game of Life PPT
一种在很多格子里不断进化的生命进化的小游戏。 它的规则是这样的: 对其中一个网格,如果它的邻居少于两个,则它会死于孤独;如果多于三个,它亦会因拥挤而死。只有当它的邻居数等于二或三时它才会生存到下一代;...
康威生命游戏(Game of Life)Delphi版本
这个程序是在windows平台下运行game of life最好的平台。它不仅对细胞空间没有限制、运行速度非常快、界面很友好,而且能够为你提供大量的初始细胞分布图案,你还可以任意组合这些图案达到你象要的构形,然后让这些...
# game-of-life-c51-128x64_oled_i2c c51单片机程序 game of life ,使用0.96 128x64 oled i2c 显示 立项时间:2020.4.24 当前位于总路径下,其中: 版本version0为已完成版本,可实现11x11以内的生命游戏运行显示...
Python零基础10天进阶班【08Quiz oo、game of life】
Python零基础10天进阶班【08Quiz oo、game of life】