289. Game of Life
2016-03-21 10:55
375 查看
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.
Subscribe to see which companies asked this question
BFS
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.
Subscribe to see which companies asked this question
BFS
public class Solution { public void gameOfLife(int[][] board) { for(int i=0;i<board.length;i++){ for(int j=0;j<board[0].length;j++){ int count = 0; if(i!=0){ if(j!=0){ count+=board[i-1][j-1]%2; } count+=board[i-1][j]%2; if(j!=board[0].length-1){ count+=board[i-1][j+1]%2; } } if(j!=0){ count+=board[i][j-1]%2; } if(j!=board[0].length-1){ count+=board[i][j+1]%2; } if(i!=board.length-1){ if(j!=0){ count+=board[i+1][j-1]%2; } count+=board[i+1][j]%2; if(j!=board[0].length-1){ count+=board[i+1][j+1]%2; } } if(bo 864a ard[i][j]==1){ if(count<2||count>3)board[i][j]=3; }else{ if(count==3)board[i][j]=2; } } } for(int i=0;i<board.length;i++){ for(int j=0;j<board[0].length;j++){ if(board[i][j]==3)board[i][j]=0; if(board[i][j]==2)board[i][j]=1; } } } }
相关文章推荐
- 使用Dreamweaver格式化HTML代码
- JavaScript 数字
- JavaScript 数组 JavaScript 对象
- JS弹出确认框及2个位数的正则验证
- CSS3之边框样式(动画过渡)
- CSS3之边框样式(动画过渡)
- 引入式样式表
- css border-style
- css中,如何设置前景色的透明度?_百度知道
- 织梦调用友情链接的调用及样式,文件位置为:\\include\\taglib\\flink.lib.php
- 如何去掉内联样式 通过style属性定义的(element.style)
- js 当前栏目选择代码。
- jQuery操作select下拉框的text值和value值的方法
- js调用后台使用ajax及控件
- js获取ID的一些技巧
- js inArray() jQuery.inArray( value, array )
- 防止恶意刷新前端代码,禁止鼠标右键和选择
- htmlentities和htmlspecialchars的区别(转载)
- jQuery-innerfade内部列表自动淡入…
- JQuery