翻译

根据维基百科的文章介绍:“Game of Life,简称为Life,是一个被英国数学家John Conway在1970年提出的细胞自动分裂器。”

给定一个m x n的空间,每个细胞有一个初始状态live(1)或dead(0)。每个细胞通过下面4种方式和周围的8个邻居交互(垂直、水平、交叉):

1,当前细胞为存活状态时,当周围低于2个(不包含2个)存活细胞时, 该细胞变成死亡状态。(模拟生命数量稀少)
2,当前细胞为存活状态时,当周围有2个或3个存活细胞时, 该细胞保持原样。
3,当前细胞为存活状态时,当周围有3个以上的存活细胞时,该细胞变成死亡状态。(模生命数量过多)
4,当前细胞为死亡状态时,当周围有3个存活细胞时,该细胞变成存活状态。 (模拟繁殖)

写一个函数用于计算空间的给定状态的下一个状态(当某个更新后)。

跟进:
1,你可以就地解决吗?记住这空间需要同时更新:你不能只首先更新一部分,然后用这些新的状态去更新别的细胞。
2,在这个问题,我们使用了二维数组。原则上,这个空间是无限大的,当这些变化影响到边界时,你是如何解决的?

原文

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?

分析

好久没有写算法了,都不习惯了。所以也就没解出来,参考了网上的答案,但还是说说分析过程吧。

细胞变为“活”,只有两种可能。当它本来为存活态时,邻居有2或3个时保持原来的状态,也就是“活”。还有一种是,当原本为死亡态时,邻居有3个时,也会变为“活”。

其余状态,要么是由存活态变为死亡态,要么就是保持死亡态。

我们通过加上10来保存这个细胞的状态,最后统一进行刷新,这也就是这个问题的核心。

if (count == 3 || (count == 2 && board[r][c] == 1)) board[r][c] += 10;

但因为是数组,所以也有边界。以后再用可变的数组修改吧,挺有意思的题目。两年前就听说了康威生命游戏,大家可以去看看它的Wiki。

代码

C Plus Plus

class Solution {
public:void gameOfLife(vector<vector<int>>& board) {for (int r = 0; r < board.size() ; r++) {for (int c = 0; c < board[r].size(); c++) {int count = countNeighbors(board, r, c);if (count == 3 || (count == 2 && board[r][c] == 1)) board[r][c] += 10;}}flashBoard(board);}int countNeighbors(const vector<vector<int>>& board, int r, int c) {int count = 0;for (int row  = max(r - 1, 0) ; row <= min(r + 1, (int) board.size() - 1); row ++) {for (int col = max(c - 1, 0); col <= min(c + 1, (int) board[row].size() - 1); col ++) {if (row != r || col != c) count += board[row][col] % 10;}}return count;}void flashBoard(vector<vector<int>>& board) {for (int i = 0; i < board.size(); i++) {for (int j = 0; j < board[i].size(); j++) {board[i][j] = board[i][j] / 10;}}}
};

Java

updated at 2016/09/04
    public void gameOfLife(int[][] board) {for (int r = 0; r < board.length; r++) {for (int c = 0; c <board[r].length; c++) {int count = countNeighbors(board, r, c);if (count == 3 || (count == 2 && board[r][c] == 1))board[r][c] += 10;}}flashBoard(board);}int countNeighbors(int[][] board, int r, int c) {int count = 0;for (int row = Math.max(r - 1, 0); row <= Math.min(r + 1, (int)board.length - 1); row++) {for (int col = Math.max(c - 1, 0); col <= Math.min(c + 1, (int)board[row].length - 1); col++) {if (row != r || col != c)count += board[row][col] % 10;}}return count;}void flashBoard(int[][] board) {for (int i = 0; i < board.length; i++) {for (int j = 0; j < board[i].length; j++) {board[i][j] = board[i][j] / 10;System.out.println("b = " + board[i][j]);}}}

LeetCode 289 Game of Life(生命游戏)(Array)相关推荐

  1. LeetCode 289. 生命游戏(位运算)

    文章目录 1. 题目 2. 解题 2.1 复制数组解法 2.2 原地解法 2.3 位运算 1. 题目 链接:289. 生命游戏 2. 解题 2.1 复制数组解法 很简单,按照题意模拟即可 class ...

  2. LeetCode 289. 生命游戏

    289. 生命游戏 思路:数每个格子周围Cell存活数 笔记:vector复制 vector<vector<int> > tmp(board); class Solution ...

  3. 【leetcode】289.生命游戏 (三种解法开阔思路,java实现)

    289. 生命游戏 难度中等 根据 百度百科 ,生命游戏,简称为生命,是英国数学家约翰·何顿·康威在 1970 年发明的细胞自动机. 给定一个包含 m × n 个格子的面板,每一个格子都可以看成是一个 ...

  4. Leetcode 289:生命游戏(最详细的解法!!!)

    根据百度百科,生命游戏,简称为生命,是英国数学家约翰·何顿·康威在1970年发明的细胞自动机. 给定一个包含 m × n 个格子的面板,每一个格子都可以看成是一个细胞.每个细胞具有一个初始状态 liv ...

  5. [Leetcode] 第289题 生命游戏

    一.题目描述 根据百度百科,生命游戏,简称为生命,是英国数学家约翰·何顿·康威在1970年发明的细胞自动机. 给定一个包含 m × n 个格子的面板,每一个格子都可以看成是一个细胞.每个细胞具有一个初 ...

  6. [leetcode][289. 生命游戏]

    根据百度百科,生命游戏,简称为生命,是英国数学家约翰·何顿·康威在1970年发明的细胞自动机. 给定一个包含 m × n 个格子的面板,每一个格子都可以看成是一个细胞.每个细胞具有一个初始状态 liv ...

  7. LeetCode 0289. 生命游戏

    [LetMeFly]289.生命游戏 力扣题目链接:https://leetcode.cn/problems/game-of-life/ 根据 百度百科 , 生命游戏 ,简称为 生命 ,是英国数学家约 ...

  8. [Leedcode][JAVA][第289题][生命游戏]

    [问题描述] 根据 百度百科 ,生命游戏,简称为生命,是英国数学家约翰·何顿·康威在 1970 年发明的细胞自动机.给定一个包含 m × n 个格子的面板,每一个格子都可以看成是一个细胞.每个细胞都具 ...

  9. 289. 生命游戏。

    根据 百度百科 , 生命游戏 ,简称为 生命 ,是英国数学家约翰·何顿·康威在 1970 年发明的细胞自动机. 给定一个包含 m × n 个格子的面板,每一个格子都可以看成是一个细胞.每个细胞都具有一 ...

最新文章

  1. linux crontab 说明
  2. ZStack源码剖析之核心库鉴赏——FlowChain
  3. 对英文文档中的单词与词组进行频率统计
  4. 廖Python学习笔记一
  5. 关于ResultSet can not re-read row data for column 1 解决方法
  6. python生成随机数代码_Python中产生随机数
  7. redis的scan命令的源码分析,实现原理
  8. Maven plugins和pluginManagement的区别概述
  9. 下一个游戏新风口已来?小游戏或成2018年最大游戏黑马
  10. 河南云计算和大数据“十三五”发展规划发布
  11. 嵌入式linux系统中设备驱动程序
  12. python练习,随机数字 函数,循环,if,格式化输出
  13. 重庆市大学生程序设计比赛相关情况
  14. 解决linux下fflush(stdin)无效
  15. div图片垂直居中 如何使div中图片垂直居中
  16. java 调用打印机 api_java 调用打印机API无法打印,但是直接打印可以,请问有人遇到过这样的问题吗?...
  17. 如此方便的日志分析功能,快来嵌入到你的网站吧!
  18. (软考)图示法求候选键,及快捷求候选键,和数据库模式分解的表格法,及无损连接分解的快捷判别方法
  19. iis7 下php 环境配置文件,Win7下在IIS7中配置PHP的环境
  20. 深度神经网络之Keras(二)——监督学习:回归

热门文章

  1. HTML5和CSS3开发第一章课后作业
  2. python从键盘获取输入并排序_python从键盘输入数字并排序-女性时尚流行美容健康娱乐mv-ida网...
  3. ROS launch调用摄像头问题(已解决)
  4. 【java】获取昨天、今天、明天、几天前、几天后的时间
  5. GameFramework篇:StarForce流程讲解
  6. 微信小程序报“app.json”错误解决办法
  7. 英雄传说6-特别攻略1
  8. 制作席慕蓉的诗html,席慕容最经典的八首诗歌
  9. 【听讲座】蚂蚁金服首席科学家漆远:人工智能驱动的金融生活服务
  10. arduinorgb三色灯_爱上 Arduino RGB Monome 全彩LED 三色led 多色 RGB共阴 LED灯