Problem: 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:
- 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 method to compute the next state (after one update) of the board given its current state. The next state is created by applying the above rules simultaneously to every cell in the current state, where births and deaths occur simultaneously.
Example (Taken from leetcode):
Input:
[
[0,1,0],
[0,0,1],
[1,1,1],
[0,0,0]
]
Output:
[
[0,0,0],
[1,0,1],
[0,1,1],
[0,1,0]
]
Approach: It can be easily done if we use O(m x n) space, basically copy the input matrix to another matrix of same size and then according to the new matrix, we'll update the original matrix. The problem is we want to avoid the extra space.
The solution is still very simple. Right now a cell is live when the value is 1 and dead if value is 0. We can use some values say x and y other than these values to denote if a particular cell died (x) or got a life (y) just because of latest iteration.
I am gonna take -1 as x i.e. a cell is recently died. I am taking -1 as it just help while comparing for the live neighbors as what I can say if Abs(board[i][j]) == 1 then neighbor cell at [i][j] is live for this particular iteration. I am gonna take 2 as y i.e. a cell is recently got a life. There is no logic for this, I just chose a positive value to denote live cells.
Now you can see we need two loops here. In the first loop we will assign -1 and 2 according to the given rules if required and in the second loop we will convert -1 to 0 and 2 to 1.
Cheers!
Implementation in C#:
public void GameOfLife(int[][] board)
{
if (board?.Length <= 0 || board[0]?.Length <= 0)
{
return;
}
for (int i = 0; i < board.Length; ++i)
{
for (int j = 0; j < board[0].Length; ++j)
{
bool modified = this.ValidateAndApplyRule1IfNeeded(board, i, j) ||
this.ValidateAndApplyRule2IfNeeded(board, i, j) ||
this.ValidateAndApplyRule3IfNeeded(board, i, j) ||
this.ValidateAndApplyRule4IfNeeded(board, i, j);
// Just for debugging purpose
// Console.WriteLine($"Cell [{i}][{j}] is modified: {modified}");
}
}
for (int i = 0; i < board.Length; ++i)
{
for (int j = 0; j < board[0].Length; ++j)
{
board[i][j] = board[i][j] <= 0 ? 0 : 1;
}
}
}
private bool ValidateAndApplyRule1IfNeeded(int[][] board, int i, int j)
{
if (board[i][j] == 1)
{
if (this.CountLiveNeighbors(board, i, j) < 2)
{
board[i][j] = -1;
return true;
}
}
return false;
}
private bool ValidateAndApplyRule2IfNeeded(int[][] board, int i, int j)
{
if (board[i][j] == 1)
{
int liveNeighbors = this.CountLiveNeighbors(board, i, j);
if (liveNeighbors == 2 || liveNeighbors == 3)
{
return true;
}
}
return false;
}
private bool ValidateAndApplyRule3IfNeeded(int[][] board, int i, int j)
{
if (board[i][j] == 1)
{
if (this.CountLiveNeighbors(board, i, j) > 3)
{
board[i][j] = -1;
return true;
}
}
return false;
}
private bool ValidateAndApplyRule4IfNeeded(int[][] board, int i, int j)
{
if (board[i][j] == 0)
{
if (this.CountLiveNeighbors(board, i, j) == 3)
{
board[i][j] = 2;
return true;
}
}
return false;
}
private int CountLiveNeighbors(int[][] board, int i, int j)
{
int liveNeighbors = 0;
if (i > 0)
{
liveNeighbors += Math.Abs(board[i - 1][j]) == 1 ? 1 : 0;
}
if (j > 0)
{
liveNeighbors += Math.Abs(board[i][j - 1]) == 1 ? 1 : 0;
}
if (i > 0 && j > 0)
{
liveNeighbors += Math.Abs(board[i - 1][j - 1]) == 1 ? 1 : 0;
}
if (i < board.Length - 1)
{
liveNeighbors += Math.Abs(board[i + 1][j]) == 1 ? 1 : 0;
}
if (j < board[0].Length - 1)
{
liveNeighbors += Math.Abs(board[i][j + 1]) == 1 ? 1 : 0;
}
if (i < board.Length - 1 && j < board[0].Length - 1)
{
liveNeighbors += Math.Abs(board[i + 1][j + 1]) == 1 ? 1 : 0;
}
if (i < board.Length - 1 && j > 0)
{
liveNeighbors += Math.Abs(board[i + 1][j - 1]) == 1 ? 1 : 0;
}
if (i > 0 && j < board[0].Length - 1)
{
liveNeighbors += Math.Abs(board[i - 1][j + 1]) == 1 ? 1 : 0;
}
return liveNeighbors;
}
Complexity: O(mxn)