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?
題目標簽:Array
題目給了我們一個2d array,讓我們根據game of life 的規則,改變cells,進行到下一個 狀態。
規定了我們要in-place改動,但是問題在於,每一個cell 的改動都依賴於它周圍8個的cell的情況,所以有先后順序的改動,肯定會造成錯誤。
所以我們要想出一種方法,就是,改動完了的cell,我們查看它的情況,依然能夠知道那個cell 之前的state。
對於每一個cell, 可以建立 4種情況:
0 dead -> dead 沒有變化
1 live -> live 沒有變化
2 live -> dead 從live 變為 dead
3 dead -> live 從dead 變為live
對於情況0 和1, 因為沒有變化,所以每個cell 的值 還是 0 和 1;
增加2種情況:如果是從 live 變為 dead, 就把值改成2; 如果是從 dead 變為 live, 就把值改成3。
那么對於每一個cell, 我們要查看的是它周圍有幾個live cell 來做判斷,如果周圍的cell 已經被改動過了,我們需要看的是它之前的狀態。
那么我們看,紅色的是初始的狀態,藍色的是改動過的狀態,因為我們只需要查看live, 不管它有沒有被改動過,我們只看紅色部分就對了,因為我們要的是它初始的狀態,那么只可能是1 和 2。所以我們只需要查看所有cell 是1 或者是 2的,來count 一共有幾個live neighbors。
Java Solution:
Runtime beats 10.94%
完成日期:09/15/2017
關鍵詞:Array,
關鍵點:對於每一個cell,有4個值,每一個值對應一種變化關系
1 class Solution 2 { 3 public void gameOfLife(int[][] board) 4 { 5 if(board == null || board.length == 0) 6 return; 7 8 int m = board.length; // rows 9 int n = board[0].length; // columns 10 11 // first iteration: mark states for each cell 12 for(int i=0; i<m; i++) // rows 13 { 14 for(int j=0; j<n; j++) // columns 15 { 16 int cnt = 0; 17 // count cell's live neighbors 3x3 matrix and set boundary 18 for(int x= Math.max(0, i-1); x<= Math.min(m-1, i+1); x++) 19 { 20 for(int y= Math.max(0, j-1); y<= Math.min(n-1, j+1); y++) 21 { 22 if(x == i && y == j) // skip itself 23 continue; 24 // only state 1 and 2: cell are live for previous state 25 if(board[x][y] == 1 || board[x][y] == 2) 26 cnt++; 27 } 28 } 29 30 if(board[i][j] == 0 && cnt == 3) // current is dead cell 31 board[i][j] = 3; // dead -> live 32 else if(board[i][j] == 1 && (cnt < 2 || cnt > 3)) // current live cell 33 board[i][j] = 2; // live -> dead 34 } 35 } 36 37 // second iteration: convert state back to dead or live 38 for(int i=0; i<m; i++) 39 for(int j=0; j<n; j++) 40 board[i][j] %= 2; 41 } 42 }
參考資料:
http://www.cnblogs.com/grandyang/p/4854466.html
LeetCode 題目列表 - LeetCode Questions List