zoukankan      html  css  js  c++  java
  • [LC] 289. Game of Life

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

    1. Any live cell with fewer than two live neighbors dies, as if caused by under-population.
    2. Any live cell with two or three live neighbors lives on to the next generation.
    3. Any live cell with more than three live neighbors dies, as if by over-population..
    4. 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. 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]
    ]
    class Solution {
        /***
        0, 1, current dead, live
        2nd bit means next live
        00 current dead -> next dead
        01 current live -> next dead
        10 current Dead -> next live
        11 current live -> next live   
        
        */
        public void gameOfLife(int[][] board) {
            if (board == null || board.length == 0 || board[0].length == 0) {
                return;
            }
            int row = board.length; 
            int col = board[0].length; 
            for (int i = 0; i < row; i++) {
                for (int j = 0; j < col; j++) {
                    int count = countCell(board, i, j);
                    if (board[i][j] == 1) {
                        // for live, case 2
                        if (count == 2 || count == 3) {
                            // left shift
                            board[i][j] += 2;
                        }
                    } // for live, case4 
                    else if (count == 3) {
                        board[i][j] += 2;
                    }
                }
            }
            
            // right shift to first bit status
            for (int i = 0; i < row; i++) {
                for (int j = 0; j < col; j++) {
                    board[i][j] = board[i][j] >> 1;
                }
            }
        }
        
        private int countCell(int[][] board, int m, int n) {
            int count = 0;
           // need to include ==, otherwise cannot reach m+ 1/ n + 1
            for (int i = Math.max(m - 1, 0); i <= Math.min(m + 1, board.length - 1); i++) {
                for (int j = Math.max(n - 1, 0); j<= Math.min(n + 1, board[0].length - 1); j++) {
                    if (i == m && j == n) {
                        continue;
                    }
                    if ((board[i][j] & 1) == 1) {
                        count += 1;
                    }
                }
            }
            return count;
        }
    }
  • 相关阅读:
    【linux基础】usleep和sleep的区别
    【算法基础】opencv函数approxPolyDP和Ramer-Douglas-Peucker Algorithm
    【算法基础】散点轮廓算法-Alpha Shapes
    【图像处理算法基础】图像分割经典算法-泛洪算法FloodFill
    .net core 部署在Linux系统上运行的环境搭建
    Linux部署Net Core网站,三种自定义绑定端口号的方法(UseUrls,UseKestrel,手动指定)
    .NetCore部署Linux环境搭建
    .Net Core 项目发布到Linux
    .Net Core 项目发布到Linux
    Linux桌面操作系统排行榜
  • 原文地址:https://www.cnblogs.com/xuanlu/p/12266811.html
Copyright © 2011-2022 走看看