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?

分析

題目很長(zhǎng),就是給你個(gè)二維數(shù)組,初始值都是0或1表示狀態(tài)响禽,狀態(tài)會(huì)隨著周圍的8個(gè)數(shù)的和不同而變化录粱,讓你在原數(shù)組上進(jìn)行狀態(tài)改變悟民,改變的時(shí)候要注意每個(gè)點(diǎn)都應(yīng)該是同時(shí)改變的米母,如果先改變一個(gè)再改變下一個(gè)會(huì)導(dǎo)致信息改變斩祭。
改變規(guī)則:

現(xiàn)狀態(tài)    鄰居和    狀態(tài)變化
1          <2        1->0
1         [2,3]      1->1
1          >3        1->0
0          =3        0->1

主要難點(diǎn)在原地上改變汹碱,又不能逐一判斷改變。
這題使用狀態(tài)記錄法(自己起的名)项玛。原來的狀態(tài)可以是0,1貌笨,改成兩位計(jì)數(shù),第一位為下一個(gè)狀態(tài)襟沮,第二位為現(xiàn)在的狀態(tài)锥惋,就得到四個(gè)狀態(tài)
00 01 10 11
得到現(xiàn)在狀態(tài)的值 &1
得到下一個(gè)狀態(tài)的值 >>1

因?yàn)槟J(rèn)下一個(gè)狀態(tài)為0,所以只需要考慮下一個(gè)狀態(tài)是1的情況

代碼

public void gameOfLife(int[][] board) {
    int row = board.length;
    if(row == 0) return ;
    int col = board[0].length;

    for(int i = 0; i < row; ++i){
        for(int j = 0; j < col; ++j){
            int sum = getNeighborSum(board, row, col, i, j);

            if(board[i][j] == 1 && sum >= 2 && sum <= 3){
                board[i][j] = 3;
            }else if(board[i][j] == 0 && sum == 3){
                board[i][j] = 2;
            }
        }
    }

    for(int i = 0; i < row; ++i){
        for(int j = 0; j < col; ++j){
            board[i][j] = board[i][j] >> 1;
        }
    }
}

//有界則取臨界值
public int getNeighborSum(int[][] board, int row, int col, int m, int n){
    int sum = 0;
    for(int x = Math.max(0, m -1); x <= Math.min(row-1, m+1); ++x){
        for(int y = Math.max(0, n-1); y <= Math.min(col-1, n+1); ++y){
            sum += board[x][y] & 1;
        }
    }
    return sum - board[m][n];
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末开伏,一起剝皮案震驚了整個(gè)濱河市膀跌,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌固灵,老刑警劉巖捅伤,帶你破解...
    沈念sama閱讀 223,002評(píng)論 6 519
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異巫玻,居然都是意外死亡丛忆,警方通過查閱死者的電腦和手機(jī)困介,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,357評(píng)論 3 400
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來蘸际,“玉大人,你說我怎么就攤上這事徒扶×竿” “怎么了?”我有些...
    開封第一講書人閱讀 169,787評(píng)論 0 365
  • 文/不壞的土叔 我叫張陵姜骡,是天一觀的道長(zhǎng)导坟。 經(jīng)常有香客問我,道長(zhǎng)圈澈,這世上最難降的妖魔是什么惫周? 我笑而不...
    開封第一講書人閱讀 60,237評(píng)論 1 300
  • 正文 為了忘掉前任,我火速辦了婚禮康栈,結(jié)果婚禮上递递,老公的妹妹穿的比我還像新娘。我一直安慰自己啥么,他們只是感情好登舞,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,237評(píng)論 6 398
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著悬荣,像睡著了一般菠秒。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上氯迂,一...
    開封第一講書人閱讀 52,821評(píng)論 1 314
  • 那天践叠,我揣著相機(jī)與錄音,去河邊找鬼嚼蚀。 笑死禁灼,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的轿曙。 我是一名探鬼主播匾二,決...
    沈念sama閱讀 41,236評(píng)論 3 424
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼拳芙!你這毒婦竟也來了察藐?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 40,196評(píng)論 0 277
  • 序言:老撾萬榮一對(duì)情侶失蹤舟扎,失蹤者是張志新(化名)和其女友劉穎分飞,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體睹限,經(jīng)...
    沈念sama閱讀 46,716評(píng)論 1 320
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡譬猫,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,794評(píng)論 3 343
  • 正文 我和宋清朗相戀三年讯檐,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片染服。...
    茶點(diǎn)故事閱讀 40,928評(píng)論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡别洪,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出柳刮,到底是詐尸還是另有隱情挖垛,我是刑警寧澤,帶...
    沈念sama閱讀 36,583評(píng)論 5 351
  • 正文 年R本政府宣布秉颗,位于F島的核電站痢毒,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏蚕甥。R本人自食惡果不足惜哪替,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,264評(píng)論 3 336
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望菇怀。 院中可真熱鬧凭舶,春花似錦、人聲如沸爱沟。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,755評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)钥顽。三九已至义屏,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間蜂大,已是汗流浹背闽铐。 一陣腳步聲響...
    開封第一講書人閱讀 33,869評(píng)論 1 274
  • 我被黑心中介騙來泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留奶浦,地道東北人兄墅。 一個(gè)月前我還...
    沈念sama閱讀 49,378評(píng)論 3 379
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像澳叉,于是被迫代替她去往敵國(guó)和親隙咸。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,937評(píng)論 2 361

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