LeetCode:Game of Life
2016-01-14 09:48
197 查看
题目描述:
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?
思路:
题目要求只能原地解答,不能先计算一部分数据,再根据计算的一部分数据计算另一部分数据。所给数据又是二维数组形式,所以,我想到了类似矩阵相乘的方法。
1、生成和给定数组相同大小的新的数组popu,用来统计每个数值对应的人的邻居个数。
2、根据每个人的邻居个数,重新计算原来的人是死是活。
若原来的人是活的,邻居个数等于2或3, 这个人活着,否则死去。
若原来的人是死的,邻居个数等于3,这个人活过来,否则仍然死的。
代码:
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?
思路:
题目要求只能原地解答,不能先计算一部分数据,再根据计算的一部分数据计算另一部分数据。所给数据又是二维数组形式,所以,我想到了类似矩阵相乘的方法。
1、生成和给定数组相同大小的新的数组popu,用来统计每个数值对应的人的邻居个数。
2、根据每个人的邻居个数,重新计算原来的人是死是活。
若原来的人是活的,邻居个数等于2或3, 这个人活着,否则死去。
若原来的人是死的,邻居个数等于3,这个人活过来,否则仍然死的。
代码:
class Solution { public: void gameOfLife(vector<vector<int>>& board) { int m = board.size(); vector<int> board0 = board[0]; int n =board0.size(); vector<vector<int>> popu; for(int i = 0; i < m; i ++) { vector<int> popSingle; bool iDecrese = i - 1 >= 0; bool iIncrese = i + 1 < m; for(int j = 0; j < n; j ++) { int pop = 0; bool jDecrese = j - 1 >= 0; bool jIncrease = j + 1 < n; if(iDecrese) { pop += board[i - 1][j] == 1; if(jDecrese) pop += board[i - 1][j - 1] == 1; if(jIncrease) pop += board[i - 1][j + 1] == 1; } if(iIncrese) { pop += board[i + 1][j] == 1; if(jDecrese) pop += board[i + 1][j - 1] == 1; if(jIncrease) pop += board[i + 1][j + 1] == 1; } if(jDecrese) pop += board[i][j - 1] == 1; if(jIncrease) pop += board[i][j + 1] == 1; popSingle.push_back(pop); } popu.push_back(popSingle); } for(int i = 0; i < m; i ++) { for(int j = 0; j < n; j ++) { int pri = board[i][j]; int bour = popu[i][j]; board[i][j] = ((pri == 1) && (bour == 2 || bour == 3)) || ((pri == 0) && (bour == 3)) ? 1: 0; } } } };
相关文章推荐
- jsp+ajax实现无刷新上传文件的方法
- Effective Java 读书笔记或学习笔记
- HTML头文件
- javascript实现二级下拉框(简单的方式)
- 阿里巴巴技术文章分享 Javascript继承机制的实现
- js转换Date日期格式
- 跟我一起学extjs5(11--自己定义模块的设计)
- HTML网页调用本地QQ
- javascript (function(){})()是个什么鬼?!
- [转]Jquery easyui开启行编辑模式增删改操作
- html入门
- AngularJS初始化静态模板详解
- Could not load the "defaultimg" image referenced from a nib in the bundle with identifier "com.abc"
- javascript中的比较
- HTML-CSS设置超链接颜色字体
- The First Few&nb…
- 基于jQuery实现文本框只能输入数字(小数、整数)
- css命名规范
- jquery拖拽效果完整实例(附demo源码下载)
- HTML5、JavaScript 3D游戏引擎和框架