Game of Life
来源:互联网 发布:固态硬盘克隆软件 编辑:程序博客网 时间:2024/05/17 23:17
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.
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]; } } } }};
0 0
- Game of Life
- Game Of Life
- LeetCode:Game of Life
- LeetCode: Game of Life
- Game of Life
- leetcode289 : Game of Life
- 【LeetCode】Game of Life
- Game of Life
- Game of Life
- LeetCode Game of Life
- Leetcode Game of Life
- Game of Life
- Game of Life
- [289]Game of Life
- [LeetCode] Game of Life
- Game Of Life
- LeetCode -- Game of Life
- 【leetcode】Game of Life
- UE4的射线拾取
- Java中的String与常量池
- printf() 输出到哪里了
- 文章标题
- mongodb
- Game of Life
- Chapter 3-02
- 如何判断微信内置浏览器 MicroMessenger
- 2015-10-10 发现的问题及解决
- 将Ubuntu等linux系统安装到移动硬盘--操作系统随身携带
- 2016届360公司PHP服务端开发笔试和面试之所得所感
- 线性表的顺序存储实验程序
- SharedPreferences 存取数据
- IOS开发 导航控制器全屏滑动返回效果