【LeetCode从零单刷】Game of Life
2015-11-02 21:53
323 查看
题目:
According to the
Wikipedia's article: "The Game of Life, also known simply asLife, 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 statelive (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?
解答:
这道题的关键在于 in-place,只能开辟常数级别的新空间。因此现有的状态需要包含上个状态的信息,设计中间信息格式以求增加信息量。最后将中间信息格式解码成正常格式。是一种时间换空间的做法。
因此0,1两种状态不够,我们需要定义更多的状态:
0:0-》0
1:1-》1
2:0-》1
3:1-》0
除此之外,我还想特别说一个问题:邻域检测,尤其当不知道邻域元素个数的情况(图像处理中尤其常见)。
我以前比较笨的方法单独处理各种情况:角落元素、边界非角落元素、非边界元素……非常麻烦而且容易错。
正确的处理方法:循环访问,但是循环范围起始点可变。每次计算 minrow~maxrow,mincol~maxcol。注意:请不要计算该元素自身。
According to the
Wikipedia's article: "The Game of Life, also known simply asLife, 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 statelive (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?
解答:
这道题的关键在于 in-place,只能开辟常数级别的新空间。因此现有的状态需要包含上个状态的信息,设计中间信息格式以求增加信息量。最后将中间信息格式解码成正常格式。是一种时间换空间的做法。
因此0,1两种状态不够,我们需要定义更多的状态:
0:0-》0
1:1-》1
2:0-》1
3:1-》0
除此之外,我还想特别说一个问题:邻域检测,尤其当不知道邻域元素个数的情况(图像处理中尤其常见)。
我以前比较笨的方法单独处理各种情况:角落元素、边界非角落元素、非边界元素……非常麻烦而且容易错。
正确的处理方法:循环访问,但是循环范围起始点可变。每次计算 minrow~maxrow,mincol~maxcol。注意:请不要计算该元素自身。
class Solution { public: int next(int i, int j, vector<vector<int>> board, int cur) { int rowsize = board.size(); int colsize = board[0].size(); int minrow = i > 0 ? (i-1) : 0; int maxrow = i < rowsize - 1 ? (i+1) : (rowsize-1); int mincol = j > 0 ? (j-1) : 0; int maxcol = j < colsize - 1 ? (j+1) : (colsize-1); int neighborlive = 0; for(int a = minrow; a <= maxrow; a++){ for(int b = mincol; b <= maxcol; b++){ if(a == i && b == j) continue; if(board[a][b] == 1 || board[a][b] == 3) neighborlive++; } } if(cur == 1){ if(neighborlive < 2 || neighborlive > 3) return 3; else return 1; } else{ if(neighborlive ==3) return 2; else return 0; } } void gameOfLife(vector<vector<int>>& board) { int rowsize = board.size(); int colsize = board[0].size(); // encoding for(int i = 0; i< rowsize; i++){ for(int j = 0; j< colsize; j++){ board[i][j] = next(i, j, board, board[i][j]); } } // decoding for(int i = 0; i< rowsize; i++){ for(int j = 0; j< colsize; j++){ if (board[i][j] == 3) board[i][j] = 0; if (board[i][j] == 2) board[i][j] = 1; } } } };
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- 关于指针的一些事情
- c++ primer 第五版 笔记前言
- share_ptr的几个注意点
- Lua中调用C++函数示例
- Lua教程(一):在C++中嵌入Lua脚本
- Lua教程(二):C++和Lua相互传递数据示例
- C++联合体转换成C#结构的实现方法
- C++编写简单的打靶游戏
- C++ 自定义控件的移植问题
- C++变位词问题分析
- C/C++数据对齐详细解析
- C++基于栈实现铁轨问题
- C++中引用的使用总结
- 使用Lua来扩展C++程序的方法
- C++中调用Lua函数实例
- Lua和C++的通信流程代码实例
- C与C++之间相互调用实例方法讲解
- 解析C++中派生的概念以及派生类成员的访问属性
- C++ Custom Control控件向父窗体发送对应的消息