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

一刷
題解:

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

對每一個cell铺董,計算出周圍活著的cell數(shù)目,并且修改2nd bit
最后全部往右移一位, 得到next status

class Solution {
    public void gameOfLife(int[][] board) {
        if (board == null || board.length == 0) return;
        int m = board.length, n = board[0].length;

        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                int lives = liveNeighbors(board, m, n, i, j);

                // In the beginning, every 2nd bit is 0;
                // So we only need to care about when will the 2nd bit become 1.
                if (board[i][j] == 1 && lives >= 2 && lives <= 3) {  
                    board[i][j] = 3; // Make the 2nd bit 1: 01 ---> 11
                }
                if (board[i][j] == 0 && lives == 3) {
                    board[i][j] = 2; // Make the 2nd bit 1: 00 ---> 10
                }
            }
        }

        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                board[i][j] >>= 1;  // Get the 2nd state.
            }
        }
    }

    public int liveNeighbors(int[][] board, int m, int n, int i, int j) {
        int lives = 0;
        for (int x = Math.max(i - 1, 0); x <= Math.min(i + 1, m - 1); x++) {
            for (int y = Math.max(j - 1, 0); y <= Math.min(j + 1, n - 1); y++) {
                lives += board[x][y] & 1;
            }
        }
        lives -= board[i][j] & 1;
        return lives;
    }

}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市齐莲,隨后出現(xiàn)的幾起案子勾怒,更是在濱河造成了極大的恐慌车猬,老刑警劉巖洪唐,帶你破解...
    沈念sama閱讀 223,207評論 6 521
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異佛致,居然都是意外死亡贮缕,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,455評論 3 400
  • 文/潘曉璐 我一進店門俺榆,熙熙樓的掌柜王于貴愁眉苦臉地迎上來感昼,“玉大人,你說我怎么就攤上這事罐脊《ㄉぃ” “怎么了?”我有些...
    開封第一講書人閱讀 170,031評論 0 366
  • 文/不壞的土叔 我叫張陵爹殊,是天一觀的道長蜕乡。 經(jīng)常有香客問我奸绷,道長梗夸,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 60,334評論 1 300
  • 正文 為了忘掉前任号醉,我火速辦了婚禮反症,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘畔派。我一直安慰自己铅碍,他們只是感情好,可當我...
    茶點故事閱讀 69,322評論 6 398
  • 文/花漫 我一把揭開白布线椰。 她就那樣靜靜地躺著胞谈,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上烦绳,一...
    開封第一講書人閱讀 52,895評論 1 314
  • 那天卿捎,我揣著相機與錄音,去河邊找鬼径密。 笑死午阵,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的享扔。 我是一名探鬼主播底桂,決...
    沈念sama閱讀 41,300評論 3 424
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼惧眠!你這毒婦竟也來了籽懦?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 40,264評論 0 277
  • 序言:老撾萬榮一對情侶失蹤锉试,失蹤者是張志新(化名)和其女友劉穎猫十,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體呆盖,經(jīng)...
    沈念sama閱讀 46,784評論 1 321
  • 正文 獨居荒郊野嶺守林人離奇死亡拖云,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,870評論 3 343
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了应又。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片宙项。...
    茶點故事閱讀 40,989評論 1 354
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖株扛,靈堂內(nèi)的尸體忽然破棺而出尤筐,到底是詐尸還是另有隱情,我是刑警寧澤洞就,帶...
    沈念sama閱讀 36,649評論 5 351
  • 正文 年R本政府宣布盆繁,位于F島的核電站,受9級特大地震影響旬蟋,放射性物質(zhì)發(fā)生泄漏油昂。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 42,331評論 3 336
  • 文/蒙蒙 一倾贰、第九天 我趴在偏房一處隱蔽的房頂上張望冕碟。 院中可真熱鬧,春花似錦匆浙、人聲如沸安寺。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,814評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽挑庶。三九已至言秸,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間迎捺,已是汗流浹背井仰。 一陣腳步聲響...
    開封第一講書人閱讀 33,940評論 1 275
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留破加,地道東北人俱恶。 一個月前我還...
    沈念sama閱讀 49,452評論 3 379
  • 正文 我出身青樓,卻偏偏與公主長得像范舀,于是被迫代替她去往敵國和親合是。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 45,995評論 2 361

推薦閱讀更多精彩內(nèi)容

  • According to the Wikipedia's article: "The Game of Life, ...
    exialym閱讀 288評論 0 0
  • 題目 According to the Wikipedia's article: "The Game of Lif...
    yxwithu閱讀 164評論 0 0
  • 289-Game of Life**QuestionEditorial Solution My Submissio...
    番茄曉蛋閱讀 393評論 0 0
  • 文學(xué)批評試圖找到文學(xué)當中的規(guī)律锭环,并以其來促進文學(xué)的發(fā)展聪全。而游戲當中固然也能找到一般規(guī)律,從《神界》到《龍騰世紀3》...
    酒劍琴詩閱讀 313評論 0 2
  • 也許邁出的第一步并沒有讓我們朝向夢想的方向更近一步辅辩,而是給我們帶來更多的驚喜难礼,超越了我們曾經(jīng)所想像的最好的程度! ???
    納蘭曦和閱讀 310評論 0 4