摘要:問題解答我的解法是需要最多的空間的。如果要做到那么我看到里一個非常的做法是用一個的數表示改變前和改變后的狀態。
問題:
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?
解答:
我的解法是需要最多o(mn)的空間的。
class Point { int x, y; public Point(int x, int y) { this.x = x; this.y = y; } } public void checkAlive(int[][] board, int val, int i, int j, Listlist) { int count = 0; int[][] dir = {{-1, -1}, {-1, 0}, {-1, 1}, {0, -1}, {0, 1}, {1, -1}, {1, 0}, {1, 1}}; for (int k = 0; k < dir.length; k++) { int x = i + dir[k][0], y = j + dir[k][1]; if (x < 0 || x > board.length - 1 || y < 0 || y > board[0].length - 1) continue; count += board[x][y]; } if (val == 1 && (count < 2 || count > 3)) { list.add(new Point(i, j)); } if (val == 0 && count == 3) { list.add(new Point(i, j)); } } public void gameOfLife(int[][] board) { if (board == null || board.length == 0 || board[0].length == 0) return; int m = board.length, n = board[0].length; List list = new ArrayList (); for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { checkAlive(board, board[i][j], i, j, list); } } for (int i = 0; i < list.size(); i++) { Point p = list.get(i); board[p.x][p.y] = board[p.x][p.y] == 1 ? 0 : 1; } }
如果要做到in space, 那么我看到discussion里一個非常tricky的做法是用一個2-bit的數表示改變前和改變后的狀態。改變前有兩個狀態:00, 01. 在滿足一定的條件后,改變前一個bit的狀態也有兩種:10, 11.所以我們只要記錄改變的狀態,然后將實際的數向后移動一位就可以得到當然的一狀態。
public int helper(int[][] board, int i, int j) { int count = 0; int[][] dir = {{-1, -1}, {-1, 0}, {-1, 1}, {0, -1}, {0, 1}, {1, -1}, {1, 0}, {1, 1}}; for (int k = 0; k < dir.length; k++) { int x = i + dir[k][0], y = j + dir[k][1]; if (x < 0 || x > board.length - 1 || y < 0 || y > board[0].length - 1) continue; count += board[x][y] & 1; } return count; } public void gameOfLife(int[][] board) { if (board == null || board.length == 0 || board[0].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 = helper(board, i, j); if (board[i][j] == 1 && (lives == 2 || lives == 3)) { board[i][j] = 3; } if (board[i][j] == 0 && lives == 3) { board[i][j] = 2; } } } for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { board[i][j] >>= 1; } } }
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://m.specialneedsforspecialkids.com/yun/66212.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 ...
摘要:板上的每個小格子有兩種狀態,或。而根據游戲規則,每一次這個板上的內容將會隨著前一次板上的內容發生變化。然后再根據當前格子的狀態計算當前格子的下一個狀態。當然最后別忘了將原始狀態傳遞出去。 題目要求 According to the Wikipedias article: The Game of Life, also known simply as Life, is a cellular...
摘要:思路普通解法,遍歷每一個細胞求值,用一個的矩陣存放結果。求值過程,稍微分析一下可知,其實就是按照以下的矩陣進行結果是可數的。 According to the Wikipedias article: The Game of Life, also knownsimply as Life, is a cellular automaton devised by the Britishmath...
吃豆人和削蘋果這兩個游戲想必大家都知道吧,本文運用Python里的Pygame控制模塊編寫出一個融合吃豆人+切水果的新手游:玩命吃蘋果,有興趣的話可以認識一下 引言 哈哈哈!木木子今天浮現——早已來給大家看了不少具體內容啦~ 涉及到的人工智能、新手、網絡爬蟲、數據統計分析(這一塊的通常但是審批)手機游戲... PS: 吃豆人我寫過了哈 Python+Pygame實戰之吃豆豆游戲的實...
摘要:如果多核的機器如何優化因為是多核,我們可以用線程來實現并行計算。如果線程變多分塊變多,邊緣信息也會變多,開銷會增大。所以選取線程的數量是這個開銷和并行計算能力的折衷。 Game of Life I According to the Wikipedias article: The Game of Life, also known simply as Life, is a cellula...
閱讀 640·2021-08-17 10:15
閱讀 1724·2021-07-30 14:57
閱讀 1978·2019-08-30 15:55
閱讀 2820·2019-08-30 15:55
閱讀 2708·2019-08-30 15:44
閱讀 670·2019-08-30 14:13
閱讀 2386·2019-08-30 13:55
閱讀 2592·2019-08-26 13:56