zoukankan      html  css  js  c++  java
  • 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):
    
    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.
    
    Follow up: 
    Could you solve it in-place? Remember that the board needs to be updated at the same time: You cannot update some cells first and then use their updated values to update other cells.
    In this question, we represent the board using a 2D array. In principle, the board is infinite, which would cause problems when the active area encroaches the border of the array. How would you address these problems?

    编解码法

    复杂度

    时间 O(NN) 空间 O(1)

    思路

    最简单的方法是再建一个矩阵保存,不过当inplace解时,如果我们直接根据每个点周围的存活数量来修改当前值,由于矩阵是顺序遍历的,这样会影响到下一个点的计算。如何在修改值的同时又保证下一个点的计算不会被影响呢?实际上我们只要将值稍作编码就行了,因为题目给出的是一个int矩阵,大有空间可以利用。这里我们假设对于某个点,值的含义为

    [2nd bit, 1st bit] = [next state, current state]
    
    - 00  dead (next) <- dead (current)
    - 01  dead (next) <- live (current)  
    - 10  live (next) <- dead (current)  
    - 11  live (next) <- live (current) 

    To get the current state, simply do

    board[i][j] & 1
    

    To get the next state, simply do

    board[i][j] >> 1

    更多的follow up 请见 http://www.cnblogs.com/EdwardLiu/p/5079920.html

    public class Solution {
        int[][] directions = new int[][]{{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1}};
        public void gameOfLife(int[][] board) {
            if (board==null || board.length==0 || board[0].length==0) return;
            int m = board.length;
            int n = board[0].length;
            for (int i=0; i<m; i++) {
                for (int j=0; j<n; j++) {
                    int lives = 0;
                    for (int[] dir : directions) {
                        int x = i + dir[0];
                        int y = j + dir[1];
                        if (x>=0 && x<m && y>=0 && y<n) {
                            if ((board[x][y] & 1) == 1) lives++;
                        }
                    }
                    
                    //update board[i][j]
                    if (board[i][j] == 0) {
                        if (lives == 3) board[i][j] = 2;
                        else board[i][j] = 0;
                    }
                    else { //board[i][j] == 1
                        if (lives<2 || lives>3) board[i][j] = 1;
                        else board[i][j] = 3;
                    }
                }
            }
            
            //decode 
            for (int i=0; i<m; i++) {
                for (int j=0; j<n; j++) {
                    board[i][j] >>= 1;
                }
            }
        }
    }
    

      

  • 相关阅读:
    模拟死锁
    B站学习斯坦福大学Swift 语言教程 iOS11 开发【第一集】踩到的几个坑(XCode 13.2.1版本)
    数学之美番外篇:平凡而又神奇的贝叶斯方法
    joj 1753: Street Numbers
    二叉树的三种遍历(递归+非递归)
    joj 1905: Freckles
    joj 2630: A Pair of Graphs(同构图的判定)
    vue3.x 中获取dom元素
    defineProperty 和 Proxy 的区别
    vue 按钮的防抖和节流
  • 原文地址:https://www.cnblogs.com/apanda009/p/7147045.html
Copyright © 2011-2022 走看看