的我正在以下問題從本文給出了:生命遊戲程序
通過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;
}