摘要:板上的每個(gè)小格子有兩種狀態(tài),或。而根據(jù)游戲規(guī)則,每一次這個(gè)板上的內(nèi)容將會(huì)隨著前一次板上的內(nèi)容發(fā)生變化。然后再根據(jù)當(dāng)前格子的狀態(tài)計(jì)算當(dāng)前格子的下一個(gè)狀態(tài)。當(dāng)然最后別忘了將原始狀態(tài)傳遞出去。
題目要求
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?
這個(gè)內(nèi)容在維基百科上講述的非常詳細(xì),有圖文示例。
這個(gè)游戲玩家在游戲開始后是不能操作的,完全是由最初的狀態(tài)來決定最終的結(jié)果。板上的每個(gè)小格子有兩種狀態(tài),live或dead。
而根據(jù)游戲規(guī)則,每一次這個(gè)板上的內(nèi)容將會(huì)隨著前一次板上的內(nèi)容發(fā)生變化。變化的規(guī)則如下:
如果當(dāng)前格子為live,那么只要它周圍live鄰居的數(shù)量大于3個(gè)或是小于2個(gè),該格子就會(huì)變成dead狀態(tài)。
如果當(dāng)前個(gè)字為dead,那么只要它周圍live的鄰居數(shù)量正好為3個(gè),該格子就會(huì)變成live狀態(tài)。否則還是dead狀態(tài)。
現(xiàn)在輸入一個(gè)狀態(tài),讓我們更新出板子的下一個(gè)狀態(tài)。
思路和代碼這里其實(shí)用暴力查詢的話也是可以解決的。也就是每遇到一個(gè)格子就將該格子的鄰居統(tǒng)統(tǒng)遍歷一下,計(jì)算一個(gè)鄰居里面live和dead的數(shù)量。然后再根據(jù)當(dāng)前格子的狀態(tài)計(jì)算當(dāng)前格子的下一個(gè)狀態(tài)。但是這里需要一個(gè)新的m*n的數(shù)組來保存新的狀態(tài),有點(diǎn)浪費(fèi)空間。而且不能很好的利用之前的遍歷結(jié)果。所以我們可以將之前的遍歷結(jié)果用某種方式直接存到當(dāng)前的格子里,減少一些遍歷次數(shù)。
假設(shè)現(xiàn)在有這樣的一個(gè)board:
[ [1,0,1], [0,0,1], [1,1,0], ]
我們從左上角開始遍歷,我們發(fā)現(xiàn)它的周圍一個(gè)live的鄰居都沒有,因此它的狀態(tài)將會(huì)變?yōu)?。但是與此同時(shí),它的上一個(gè)狀態(tài)為1,需要傳遞給周圍的鄰居,因此我們可以采用將周圍鄰居+10的方式傳遞。如下:
[ [0,10,1], [10,10,1], [1,1,0], ]
這是我們再看坐標(biāo)為(0,1)的格子。假設(shè)當(dāng)前個(gè)字上的值為cur,那么cur/10則是之前鄰居的數(shù)量,cur%10則代表該格子的初始狀態(tài)。根據(jù)這個(gè)規(guī)律,我們可以知道當(dāng)前格子上的初始狀態(tài)為0即dead,而之前遍歷過的鄰居有1個(gè)。這時(shí)我們就無需遍歷所有的鄰居,只需要繼續(xù)訪問還未訪問的鄰居即可。當(dāng)然最后別忘了將原始狀態(tài)傳遞出去。
代碼如下:
public void gameOfLife(int[][] board) { if(board==null || board.length==0) return; int row = board.length; int column = board[0].length; int multiply = 10; for(int i = 0 ; i=0 && isLive(board[i+1][j-1])){liveNeighboars++;} if(j+1
3){ board[i][j] = 0; }else{ board[i][j] = 1; } if(j+1 =0 && i+1 |
想要了解更多開發(fā)技術(shù),面試教程以及互聯(lián)網(wǎng)公司內(nèi)推,歡迎關(guān)注我的微信公眾號!將會(huì)不定期的發(fā)放福利哦~
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/68653.html
Problem According to the Wikipedias 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 ...
摘要:問題解答我的解法是需要最多的空間的。如果要做到那么我看到里一個(gè)非常的做法是用一個(gè)的數(shù)表示改變前和改變后的狀態(tài)。 問題:According to the Wikipedias article: The Game of Life, also known simply as Life, is a cellular automaton devised by the British mathem...
摘要:思路普通解法,遍歷每一個(gè)細(xì)胞求值,用一個(gè)的矩陣存放結(jié)果。求值過程,稍微分析一下可知,其實(shí)就是按照以下的矩陣進(jìn)行結(jié)果是可數(shù)的。 According to the Wikipedias article: The Game of Life, also knownsimply as Life, is a cellular automaton devised by the Britishmath...
摘要:如果多核的機(jī)器如何優(yōu)化因?yàn)槭嵌嗪?,我們可以用線程來實(shí)現(xiàn)并行計(jì)算。如果線程變多分塊變多,邊緣信息也會(huì)變多,開銷會(huì)增大。所以選取線程的數(shù)量是這個(gè)開銷和并行計(jì)算能力的折衷。 Game of Life I According to the Wikipedias article: The Game of Life, also known simply as Life, is a cellula...
吃豆人和削蘋果這兩個(gè)游戲想必大家都知道吧,本文運(yùn)用Python里的Pygame控制模塊編寫出一個(gè)融合吃豆人+切水果的新手游:玩命吃蘋果,有興趣的話可以認(rèn)識(shí)一下 引言 哈哈哈!木木子今天浮現(xiàn)——早已來給大家看了不少具體內(nèi)容啦~ 涉及到的人工智能、新手、網(wǎng)絡(luò)爬蟲、數(shù)據(jù)統(tǒng)計(jì)分析(這一塊的通常但是審批)手機(jī)游戲... PS: 吃豆人我寫過了哈 Python+Pygame實(shí)戰(zhàn)之吃豆豆游戲的實(shí)...
閱讀 2492·2021-09-29 09:34
閱讀 3353·2021-09-23 11:21
閱讀 2528·2021-09-06 15:00
閱讀 1148·2019-08-30 15:44
閱讀 2052·2019-08-29 17:23
閱讀 3025·2019-08-29 16:44
閱讀 3082·2019-08-29 13:13
閱讀 1964·2019-08-28 18:12