[LC]289. Game of Life

来源:互联网 发布:手机能注册淘宝网店吗 编辑:程序博客网 时间:2024/06/03 21:04

一、问题描述

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.

Follow up

  1. 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.
  2. 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?

二、我的思路

暴力解…就算暴力解,代码写的也不够漂亮,另外还debug了好久啊啊啊

新开一个m*n空间存下一轮的棋盘。遍历每个点,先统计它周围活着的人数。再根据该点的情况决定新棋盘的值。

class Solution {    public void gameOfLife(int[][] board) {        int m = board.length;        int n = board[0].length;                int[][] newBoard = new int[m][n];        for(int i = 0; i < m; i ++){            for(int j = 0; j < n; j ++){                int alive = 0;                for(int k = -1; k <= 1; k += 1){                    for(int p = -1; p <= 1; p += 1){                        if(i + k >= 0 && i + k < m && j + p >= 0 && j + p < n){                            if(board[i + k][j + p] == 1){                                alive ++;                            }                        }                    }                }                                if(board[i][j] == 1){                    alive --;                    if(alive < 2 || alive > 3){                        newBoard[i][j] = 0;                    }                    else{                        newBoard[i][j] = 1;                    }                }                else{                    if(alive == 3){                        newBoard[i][j] = 1;                    }                    else{                        newBoard[i][j] = 0;                    }                }            }        }                for(int i = 0; i < m; i ++){            for(int j = 0; j < n; j ++){                 board[i][j] = newBoard[i][j];            }        }                    }}
猜想能用DP...但看了discuss并没有。。


Follow up1: 如果要in place的话,只要能保持原有的0和1的数据,并且增加下一轮是否改变原值的信息即可。所以我选用了2表示1下一轮要变0,3表示0下一轮要变1.最后处理完棋盘后,把棋盘每个格子对2取余数即可。

Follow up2没读懂…


三、淫奇技巧

思路差不多,除了记录下一轮的方法有了左移右移。但是人家代码比我的好多了,各种情况条件整合的特别好。。。


详情参照:http://www.jianshu.com/p/27024d85b5da

class Solution {public:    void gameOfLife(vector<vector<int>>& board) {        int row(board.size()), column(board[0].size());        for (int i = 0; i < row; i++) {            for (int j = 0; j < column; j++) {                int count = 0;                for (int I = max(i - 1, 0); I < min(i + 2, row); I++)                    for (int J = max(j - 1, 0); J < min(j + 2, column); J++)                        count += board[I][J] & 1;                if (count == 3 || (count == 2 && board[i][j]))                    board[i][j] |= 2;            }        }        for (int i = 0; i < row; i++)            for (int j = 0; j < column; j++)                board[i][j] >>= 1;    }};作者:Terence_F链接:http://www.jianshu.com/p/27024d85b5da來源:简书著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

四、知识点



五、问题

如果把我的代码的最后赋值段改成board = newBoard; 结果是错误的。为什么呢?

原创粉丝点击