289. 生命游戏
根据 百度百科 ,生命游戏,简称为生命,是英国数学家约翰·何顿·康威在 1970 年发明的细胞自动机。
给定一个包含 m × n 个格子的面板,每一个格子都可以看成是一个细胞。每个细胞都具有一个初始状态:1 即为活细胞(live),或 0 即为死细胞(dead)。每个细胞与其八个相邻位置(水平,垂直,对角线)的细胞都遵循以下四条生存定律:
如果活细胞周围八个位置的活细胞数少于两个,则该位置活细胞死亡;
如果活细胞周围八个位置有两个或三个活细胞,则该位置活细胞仍然存活;
如果活细胞周围八个位置有超过三个活细胞,则该位置活细胞死亡;
如果死细胞周围正好有三个活细胞,则该位置死细胞复活;
下一个状态是通过将上述规则同时应用于当前状态下的每个细胞所形成的,其中细胞的出生和死亡是同时发生的。给你 m x n 网格面板 board 的当前状态,返回下一个状态。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/game-of-life
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
import java.util.Scanner;
class Solution {
private int[][] directions = {{-1, 0}, {-1, 1}, {0, 1}, {1, 1}, {1, 0}, {1, -1}, {0, -1}, {-1, -1}};
private int count(int[][] board, int x, int y) {
int cnt = 0;
for (int i = 0; i < directions.length; ++i) {
int nx = directions[i][0] + x;
int ny = directions[i][1] + y;
if (nx >= 0 && nx < board.length && ny >= 0 && ny < board[0].length && (board[nx][ny] == 1 || board[nx][ny] == 2)) {
cnt++;
}
}
return cnt;
}
public void gameOfLife(int[][] board) {
for (int i = 0; i < board.length; ++i) {
for (int j = 0; j < board[0].length; ++j) {
int cnt = count(board, i, j);
if (board[i][j] == 1) {
if (cnt < 2 || cnt > 3) {
board[i][j] = 2;
}
} else {
if (cnt == 3) {
board[i][j] = 3;
}
}
}
}
for (int i = 0; i < board.length; ++i) {
for (int j = 0; j < board[0].length; ++j) {
if (board[i][j] == 2) {
board[i][j] = 0;
} else if (board[i][j] == 3) {
board[i][j] = 1;
}
}
}
}
}
心之所向,素履以往 生如逆旅,一苇以航

浙公网安备 33010602011771号