gpt4 book ai didi

ruby - 生命游戏计划

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:17:36 25 4
gpt4 key购买 nike

我正在研究 Leetcode 中的以下问题:

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 the beginning, every cell is either 00 or 01. Notice that 1st state is independent of 2nd state. Imagine all cells are instantly changing from the 1st to the 2nd state, at the same time. Let's count # of neighbors from 1st state and set 2nd state bit. Since every 2nd state is by default dead, no need to consider transition 01 -> 00. In the end, delete every cell's 1st state by doing >> 1. For each cell's 1st bit, check the 8 pixels around itself, and set the cell's 2nd bit.

Transition 01 -> 11: when board == 1 and lives >= 2 && lives <= 3. Transition 00 -> 10: when board == 0 and lives == 3.

我的代码失败了,我不确定原因。这是输出与预期的对比:

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

关于ruby - 生命游戏计划,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42180054/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com