Problem
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. The next state is created by applying the above rules simultaneously to every cell in the current state, where births and deaths occur simultaneously.
Example:
Input:
[ [0,1,0], [0,0,1], [1,1,1], [0,0,0] ]
Output:
[ [0,0,0], [1,0,1], [0,1,1], [0,1,0] ]
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?
class Solution { public void gameOfLife(int[][] board) { if (board == null || board.length == 0 || board[0].length == 0) return; //how to get to neighbors for board[i][j] -> use int[8][2] to store offset in 8 directions int[][] directions = {{-1,-1},{-1,0},{-1,1},{0,1},{0,-1},{1,-1},{1,0},{1,1}}; for (int i = 0; i < board.length; i++) { for (int j = 0; j < board[0].length; j++) { int lives = 0; for (int k = 0; k < directions.length; k++) { int x = i+directions[k][0]; int y = j+directions[k][1]; if (x >= 0 && x < board.length && y >= 0 && y < board[0].length && board[x][y]%2 == 1) lives++; } if (board[i][j]%2 == 1) { if (lives == 2 || lives == 3) board[i][j] += 2; } else { if (lives == 3) board[i][j] += 2; } } } //after +2 operations for all live cells, the dying cells still hold 1 or 0 for (int i = 0; i < board.length; i++) { for (int j = 0; j < board[0].length; j++) { //eliminate 1s and 0s to 0, 3s and 2s to 1 board[i][j] >>= 1; } } } }
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/77317.html
摘要:板上的每個(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 Wikipedias article: The Game of Life, also known simply as Life, is a cellular...
摘要:問題解答我的解法是需要最多的空間的。如果要做到那么我看到里一個(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í)...
閱讀 3054·2021-11-22 09:34
閱讀 3646·2021-08-31 09:45
閱讀 3859·2019-08-30 13:57
閱讀 1682·2019-08-29 15:11
閱讀 1687·2019-08-28 18:04
閱讀 3231·2019-08-28 17:59
閱讀 1570·2019-08-26 13:35
閱讀 2195·2019-08-26 10:12