According to the Wikipedia‘s article: "The Game of Life, also known simply as Life, is a cellular automaton devised by the British mathematician John Horton Conway in 1970."
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):
Write a function 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:
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]
]
Follow up:
解法1 将原矩阵复制下来,按照游戏规则修改原来的矩阵
class Solution {
public:
void gameOfLife(vector<vector<int>>& board) {
vector<vector<int>>tmp_board(board.begin(), board.end());
int m = board.size(), n = board[0].size();
for(int i = 0; i < m; ++i){
for(int j = 0; j < n; ++j){
int cnt = 0;
for(int k = 0; k < 8; ++k){
int tmp_x = i + dx[k], tmp_y = j + dy[k];
if(valid(tmp_x, tmp_y, m, n) && tmp_board[tmp_x][tmp_y]){
cnt++;
}
}
if(tmp_board[i][j] == 1){
if(cnt < 2 || cnt > 3){
board[i][j] = 0;
}else{
board[i][j] = 1;
}
}else{
if(cnt == 3)board[i][j] = 1;
else board[i][j] = 0;
}
}
}
}
private:
int dx[8] = {-1, 0, -1, -1, 0, 1, 1, 1};
int dy[8] = {0, -1, -1, 1, 1, 0, -1, 1};
bool valid(int x, int y, int m, int n){
if(x < 0 || x >= m || y < 0 || y >= n)return false;
return true;
}
};
解法2 原地修改,\(O(1)\)空间复杂度。使用多个状态:
按照行顺序更新时,对于每个cell,左、上、左上、右上是被更新了,剩下四个没有更新,按照对应的数值统计出在原始矩阵中的数字,然后更新当前cell,最后遍历一遍,把2和3分别修改成1和0
class Solution {
public:
void gameOfLife(vector<vector<int>>& board) {
int m = board.size(), n = board[0].size();
for(int i = 0; i < m; ++i){
for(int j = 0; j < n; ++j){
int cnt = 0;
for(int k = 0; k < 4; ++k){
int tmp_x = i + dx[k], tmp_y = j + dy[k];
if(valid(tmp_x, tmp_y, m, n) &&
(board[tmp_x][tmp_y] == 3 || board[tmp_x][tmp_y] == 1)){
cnt++;
}
}
for(int k = 4; k < 8; ++k){
int tmp_x = i + dx[k], tmp_y = j + dy[k];
if(valid(tmp_x, tmp_y, m, n) && board[tmp_x][tmp_y] == 1){
cnt++;
}
}
if(board[i][j] == 1){
if(cnt < 2 || cnt > 3){
board[i][j] = 3;
}else{
board[i][j] = 1;
}
}else{
if(cnt == 3)board[i][j] = 2;
else board[i][j] = 0;
}
}
}
for(int i = 0; i < m; ++i){
for(int j = 0; j < n; ++j){
if(board[i][j] == 2)board[i][j] = 1;
else if(board[i][j] == 3)board[i][j] = 0;
}
}
}
private:
int dx[8] = {-1, 0, -1, -1, 0, 1, 1, 1};
int dy[8] = {0, -1, -1, 1, 1, 0, -1, 1};
bool valid(int x, int y, int m, int n){
if(x < 0 || x >= m || y < 0 || y >= n)return false;
return true;
}
};
【刷题-LeetCode】289. Game of Life
原文:https://www.cnblogs.com/vinnson/p/13380368.html