289. Game of Life

Question

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):

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.


Code

public class Solution {
    private final int DD = 0;
    private final int LL = 1;
    private final int DL = 2;
    private final int LD = 3;
    
    public void gameOfLife(int[][] board) {
        if (board == null || board.length == 0) return;
        int m = board.length, n = board[0].length;
        
        int[] dx = {-1, 0, 1, -1, 1, -1, 0, 1};
        int[] dy = {1, 1, 1, 0, 0, -1, -1, -1};
        
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                int live = 0, die = 0;
                for (int k = 0; k < 8; k++) {
                    int nx = i + dx[k];
                    int ny = j + dy[k];
                    
                    if (nx < 0 || nx >= m || ny < 0 || ny >= n) {
                        die++;
                        continue;
                    }
                    
                    if (board[nx][ny] == 0 || board[nx][ny] == 2) die++;
                    else live++;
                }
                if (board[i][j] == 1) {
                    if (live < 2) {
                        board[i][j] = 3;
                    } else if (live > 3) {
                        board[i][j] = 3;
                    }
                } else {
                    if (live == 3) {
                        board[i][j] = 2;
                    }
                }
            }
        }
        
        
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (board[i][j] == 2) board[i][j] = 1;
                if (board[i][j] == 3) board[i][j] = 0;
            }
        }
    }
}

Solution

用4个int分别代表状态转换的情况:0表示由死到死,1表示由生到生,2表示由死到生,3表示由生到死。遍历neighbor的时候,需查看neighbor状态转换前的状态。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • According to the Wikipedia's article: "The Game of Life, ...
    Jeanz阅读 1,495评论 0 0
  • 题目 According to the Wikipedia's article: "The Game of Lif...
    yxwithu阅读 1,342评论 0 0
  • 289-Game of Life**QuestionEditorial Solution My Submissio...
    番茄晓蛋阅读 2,990评论 0 0
  • According to the Wikipedia's article: "The Game of Life, ...
    exialym阅读 2,350评论 0 0
  • 25岁以后,我掌握了超音速。 空间允许的情况下,我的奔跑速度超过地球上任何型号的超音速战机。假如给我装上翅膀,我可...
    风中的运叔阅读 3,800评论 1 1

友情链接更多精彩内容