生命游戏程序

问题描述:

的我正在以下问题从本文给出了:生命游戏程序

通过n个单元考虑到与米板,每个小区具有活的初始状态 (1)或死的(0)。每个单元使用以下四个规则(从上面的维基百科文章采取 )的八个相邻 (水平,垂直,对角线)互动:

少于两只活邻居的活细胞死亡,仿佛引起 根据人口。任何有两个或三个活着的邻居的活细胞在下一代生活 。有三个以上的活 邻居的活细胞死亡,仿佛人口过多。与恰好 三只活邻居的死细胞变活细胞,仿佛再现。写入 函数来计算给定其当前状态的板 的下一个状态(在一次更新之后)。

追问:你能解决这个问题就地?请记住,董事会需要 在同一时间进行更新:您不能更新某些细胞第一和 然后用自己的更新值更新其他细胞。

我的解决方案是在网站上的其他用户提供的解决方案之后建模的,因此添加了他们的解决方案描述。

在开始时,每个小区是00或01。请注意,第一状态是 独立第二状态的。想象一下,所有的细胞都在同时从第一个状态到第二个状态瞬间变化为 。我们来计算# 来自第一状态的邻居并设置第二状态位。由于每2状态 默认情况下死了,没有必要考虑转型01 - > 00在 最后,通过做>> 1.删除每一个细胞的第一个国家对于每个单元的第1个 位,检查自己周围的8个像素,并设置单元的第二位。

过渡01 - > 11:当板== 1和生活> = 2个& &生命< = 3 过渡00 - > 10:板== 0时,住== 3.

我的代码失败,我不知道为什么。下面是输出VS预期:

Input: 
[[0,0,0,0,0],[0,0,1,0,0],[0,0,1,0,0],[0,0,1,0,0],[0,0,0,0,0]] 
Output: 
[[0,0,0,0,0],[0,0,0,0,0],[0,1,1,1,0],[0,1,0,1,0],[0,0,1,1,0]] 
Expected: 
[[0,0,0,0,0],[0,0,0,0,0],[0,1,1,1,0],[0,0,0,0,0],[0,0,0,0,0]] 

好像后来行基于前面的行以前的更新正在更新,但我相信我占了这个..任何人都知道是什么问题?我下面的解决方案:

# @param {Integer[][]} board 
# @return {Void} Do not return anything, modify board in-place instead. 
def game_of_life(board) 
    #error conditions 
    return nil if (board.nil? || board.length == 0) #empty or nil arr 

    row = 0 
    col = 0 
    m = board.length 
    n = board[0].length 

    until row == m 
     col = 0 
     until col == n 
      live_count = adj_live_counter(board, row, col) #leaving out two conditions because by default second bit is 0 
      if alive?(board, row, col) && live_count == 2 || live_count == 3 
       board[row][col] = 3 
      elsif dead?(board, row, col) && live_count == 3 
       board[row][col] = 2 
      end 
      col+=1 
     end 
     row+=1 
    end 
    p board 
    #when the above is done, grab second bit for every cell. 
    #board = clear_first_bit(board) 
    clear_first_bit(board) 
    p board 
end 

private 

def adj_live_counter(board, row, col) 
    m = board.length 
    n = board[0].length 
    count = 0 

    r = [row - 1, 0].max #start: either 0 or the above element 
    until r > [row + 1, m - 1].min #end: below element or end of arr 
     c = [col - 1, 0].max #start: at left element or 0 
     until c > [col + 1, n - 1].min #end: at right element or end of arr 
      count += board[r][c] & 1 
      #p count 
      c += 1 
     end 
     r += 1 
    end 
    count -= board[row][col] & 1 

    count 
end 

def clear_first_bit(board) 
    m = board.length 
    n = board[0].length 

    row = 0 
    col = 0 

    until row == m 
     col = 0 
     until col == n 
      board[row][col] >>= 1 
      col += 1 
     end 
     row += 1 
    end 
end 

def alive?(board, row, count) 
    board[row][count] & 1 == 1 
end 

def dead?(board, row, count) 
    board[row][count] & 1 == 0 
end 

解决方案通过网站提供的(在Java中):

public void gameOfLife(int[][] board) { 
    if (board == null || board.length == 0) return; 
    int m = board.length, n = board[0].length; 

    for (int i = 0; i < m; i++) { 
     for (int j = 0; j < n; j++) { 
      int lives = liveNeighbors(board, m, n, i, j); 

      // In the beginning, every 2nd bit is 0; 
      // So we only need to care about when will the 2nd bit become 1. 
      if (board[i][j] == 1 && lives >= 2 && lives <= 3) { 
       board[i][j] = 3; // Make the 2nd bit 1: 01 ---> 11 
      } 
      if (board[i][j] == 0 && lives == 3) { 
       board[i][j] = 2; // Make the 2nd bit 1: 00 ---> 10 
      } 
     } 
    } 

    for (int i = 0; i < m; i++) { 
     for (int j = 0; j < n; j++) { 
      board[i][j] >>= 1; // Get the 2nd state. 
     } 
    } 
} 

public int liveNeighbors(int[][] board, int m, int n, int i, int j) { 
    int lives = 0; 
    for (int x = Math.max(i - 1, 0); x <= Math.min(i + 1, m - 1); x++) { 
     for (int y = Math.max(j - 1, 0); y <= Math.min(j + 1, n - 1); y++) { 
      lives += board[x][y] & 1; 
     } 
    } 
    lives -= board[i][j] & 1; 
    return lives; 
} 

有这些线路上的一个优先问题:

if alive?(board, row, col) && live_count == 2 || live_count == 3 
     board[row][col] = 3 

这段代码被解析为:

if (alive?(board, row, col) && live_count == 2) || live_count == 3 
     board[row][col] = 3 

如果你有一个死细胞(状态0)有3个活的邻居,你将它改为状态3 - 这意味着它将在下一个状态中活着,并且现在也处于当前状态!

试试这个:

if alive?(board, row, col) && (live_count == 2 || live_count == 3) 
     board[row][col] = 3