← 返回 scale.ai 的题目列表Neuron State Update (Game of Life Variant)
类型:online_judge
Problem: Neuron Matrix State Update (Game of Life Variant)
Given an m x n integer matrix neurons representing neuron states:
neurons[i][j] == 0 means the neuron is firing
neurons[i][j] != 0 means the neuron is non-firing (a non-negative integer value)
Perform one state update. For each cell (i, j), count the number k of its neighbours that are firing (i.e., have value 0), and update the cell according to:
If the current cell is firing (neurons[i][j] == 0):
If k == 3, the new value becomes 6
Otherwise it remains 0
If the current cell is non-firing (neurons[i][j] > 0):
If k <= 1, new value = max(0, neurons[i][j] - 2)
If k > 3, new value = max(0, neurons[i][j] - 1)
Otherwise (k is 2 or 3), it stays unchanged
Return the updated matrix.
I/O Format
Input:
First line: two integers m n
Next m lines: n integers each, the matrix neurons
Output:
m lines of the updated matrix
Constraints (typical interview assumptions)
1 <= m, n <= 1000
0 <= neurons[i][j] <= 10^9
Example
Note: the example assumes 8-neighbourhood (including diagonals). If the interview specifies 4-neighbourhood, follow that.
Input:
3 3
0 1 0
2 0 3
4 5 0
Output:
0 1 0
1 0 3
3 5 0
Follow-up (Optimization)
After solving using an auxiliary copy matrix, optimize space from O(mn) to an in-place update (extra space O(1) or O(n)).
Example
Input
1 1
0
Output
0