Game of Life
2015-10-10 00:31
363 查看
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?
Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.
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?
Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.
class Solution { public: void gameOfLife(vector<vector<int>>& board) { int m = board.size(); int n; if(m > 0){ n = board[0].size(); if(n > 0){ vector<vector<int>> changes; int i=0,row = 0, col = 0, cells = m * n; for(; i < cells; i++){ row = i / n; col = i - row * n; int total = 0; if( (row-1) >= 0){ if((col-1) >= 0){ total += board[row-1][col-1]; } total += board[row-1][col]; if((col+1) < n){ total += board[row-1][col+1]; } } if((row+1) < m){ if((col-1) >= 0){ total += board[row+1][col-1]; } total += board[row+1][col]; if((col+1) < n){ total += board[row+1][col+1]; } } if((col-1) >= 0){ total += board[row][col-1]; } if((col+1) < n){ total += board[row][col+1]; } if(board[row][col] == 1){ if(total < 2 || total > 3){ vector<int> tp; tp.push_back(row); tp.push_back(col); tp.push_back(0); changes.push_back(tp); } }else{ if(total == 3){ vector<int> tp; tp.push_back(row); tp.push_back(col); tp.push_back(1); changes.push_back(tp); } } } for(i=0; i< changes.size(); i++){ board[changes[i][0]][changes[i][1]] = changes[i][2]; } } } } };
相关文章推荐
- 基于HTML5 Canvas实现工控2D叶轮旋转
- IIFE-js中(function(){…})()立即执行函数写法理解
- js模块化历程
- jQuery的deferred对象详解
- 前端框架【Bootstrap】框架体验评测
- js继承原理
- css display的用法及原理
- JSP中使用JSTL按不同条件输出内容的方法
- JSP实现用于自动生成表单标签html代码的自定义表单标签
- jsp实现Servlet文件下载的方法
- PHP+Mysql+jQuery文件下载次数统计实例讲解
- PHP+Mysql+jQuery中国地图区域数据统计实例讲解
- 页面内容排序插件jSort使用方法
- clipboard.js无需Flash无需依赖任何JS库实现文本复制与剪切
- JS实现的车标图片提示效果代码
- JavaScript实现的浮动层框架用法实例分析
- JS实现漂亮的窗口拖拽效果(可改变大小、最大化、最小化、关闭)
- JS+DIV+CSS排版布局实现美观的选项卡效果
- jQuery实现仿新浪微博浮动的消息提示框(可智能定位)
- adt-bundle preference中添加SDK路径后 不能点击ok