码迷,mamicode.com
首页 > 编程语言 > 详细

[LeetCode][JavaScript]Game of Life

时间:2015-10-11 00:29:14      阅读:389      评论:0      收藏:0      [点我收藏+]

标签:

Game of Life

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):

 

  1. Any live cell with fewer than two live neighbors dies, as if caused by under-population.
  2. Any live cell with two or three live neighbors lives on to the next generation.
  3. Any live cell with more than three live neighbors dies, as if by over-population..
  4. 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: 

  1. 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.
  2. 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,即只在board数组中处理,不要开额外的数组空间。

整理一下规则:

1. 邻居小于2个或者大于3个就会死;

2. 邻居等于2个,状态不变,原来是什么还是什么;

3. 邻居等于3个,存活,不管原来是什么状态。

in-place主要需要解决一个问题,处理一个cell的时候,不能根据改变后的状态判断,要依据原始的状态。

第一轮处理,无非就是4种情况:0 -> 0, 1 -> 1, 0 -> 1, 1 -> 0。

其中0 -> 0, 1 -> 1没有问题,我们把0 -> 1记成2, 1 -> 0记成3。

在判断邻居的时候1和3都认为是活着的。

第二轮循环把所有的2换成1,3换成0。

 1 /**
 2  * @param {number[][]} board
 3  * @return {void} Do not return anything, modify board in-place instead.
 4  */
 5 var gameOfLife = function(board) {
 6     var i, j;
 7     for(i = 0; i < board.length; i++){
 8         for(j = 0; j < board[i].length; j++){
 9             var curr = board[i][j];
10             var neighbors = countNeighbors(i, j);
11             if(neighbors < 2 || neighbors > 3){
12                 if(curr === 1){
13                     board[i][j] = 3;
14                 }
15             }else if(neighbors === 3){
16                 if(curr === 0){
17                     board[i][j] = 2;
18                 }
19             }
20         }
21     }
22     for(i = 0; i < board.length; i++){
23         for(j = 0; j < board[i].length; j++){
24             if(board[i][j] === 2){
25                 board[i][j] = 1;
26             }else if(board[i][j] === 3){
27                 board[i][j] = 0;
28             }
29         }
30     }
31 
32 
33     function countNeighbors(i, j){
34         var count = 0;
35         count += isAlive(i, j - 1); count += isAlive(i, j + 1);
36         count += isAlive(i - 1, j - 1); count += isAlive(i - 1, j); count += isAlive(i - 1, j + 1);
37         count += isAlive(i + 1, j - 1); count += isAlive(i + 1, j); count += isAlive(i + 1, j + 1);
38         return count;
39     }
40     function isAlive(i, j){
41         if(!board[i] || !board[i][j]){
42             return 0;
43         }
44         var cell = board[i][j];
45         if(cell === 1 || cell === 3){
46             return 1;
47         }
48         return 0;
49     }
50 };

 

 

[LeetCode][JavaScript]Game of Life

标签:

原文地址:http://www.cnblogs.com/Liok3187/p/4868599.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!