|
| 1 | +// Source : https://leetcode.com/problems/game-of-life/ |
| 2 | +// Author : Hao Chen |
| 3 | +// Date : 2019-03-20 |
| 4 | + |
| 5 | +/***************************************************************************************************** |
| 6 | + * |
| 7 | + * According to the Wikipedia's article: "The Game of Life, also known simply as Life, is a cellular |
| 8 | + * automaton devised by the British mathematician John Horton Conway in 1970." |
| 9 | + * |
| 10 | + * Given a board with m by n cells, each cell has an initial state live (1) or dead (0). Each cell |
| 11 | + * interacts with its eight neighbors (horizontal, vertical, diagonal) using the following four rules |
| 12 | + * (taken from the above Wikipedia article): |
| 13 | + * |
| 14 | + * Any live cell with fewer than two live neighbors dies, as if caused by under-population. |
| 15 | + * Any live cell with two or three live neighbors lives on to the next generation. |
| 16 | + * Any live cell with more than three live neighbors dies, as if by over-population.. |
| 17 | + * Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction. |
| 18 | + * |
| 19 | + * Write a function to compute the next state (after one update) of the board given its current state. |
| 20 | + * The next state is created by applying the above rules simultaneously to every cell in the current |
| 21 | + * state, where births and deaths occur simultaneously. |
| 22 | + * |
| 23 | + * Example: |
| 24 | + * |
| 25 | + * Input: |
| 26 | + * [ |
| 27 | + * [0,1,0], |
| 28 | + * [0,0,1], |
| 29 | + * [1,1,1], |
| 30 | + * [0,0,0] |
| 31 | + * ] |
| 32 | + * Output: |
| 33 | + * [ |
| 34 | + * [0,0,0], |
| 35 | + * [1,0,1], |
| 36 | + * [0,1,1], |
| 37 | + * [0,1,0] |
| 38 | + * ] |
| 39 | + * |
| 40 | + * Follow up: |
| 41 | + * |
| 42 | + * Could you solve it in-place? Remember that the board needs to be updated at the same time: |
| 43 | + * You cannot update some cells first and then use their updated values to update other cells. |
| 44 | + * In this question, we represent the board using a 2D array. In principle, the board is |
| 45 | + * infinite, which would cause problems when the active area encroaches the border of the array. How |
| 46 | + * would you address these problems? |
| 47 | + * |
| 48 | + ******************************************************************************************************/ |
| 49 | + |
| 50 | + |
| 51 | +class Solution { |
| 52 | +public: |
| 53 | + // the problem here is we need store two states in one cell, |
| 54 | + // one is the original state, another is the new state |
| 55 | + // So, we could store the state into the bit. |
| 56 | + // - Old State: the first bit from the right |
| 57 | + // - New State: the second bit from the right |
| 58 | + void liveCheck(vector<vector<int>>& board, int r, int c) { |
| 59 | + int cnt = 0; |
| 60 | + for (int i=r-1; i<=r+1; i++) { |
| 61 | + if (i < 0 || i>=board.size()) continue; |
| 62 | + for (int j=c-1; j<=c+1; j++) { |
| 63 | + if (j<0 || j>=board[0].size() || (i==r && j==c)) continue; |
| 64 | + if ( board[i][j] & 1 ) cnt++; |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + //live -> die |
| 69 | + //if (board[r][c]==1 && (cnt < 2 || cnt > 3)) board[r][c] = 1; |
| 70 | + |
| 71 | + //live -> live |
| 72 | + if ( board[r][c] == 1 && (cnt == 2 || cnt == 3) ) board[r][c] = 3; |
| 73 | + |
| 74 | + //die -> live |
| 75 | + if ( board[r][c] == 0 && cnt == 3 ) board[r][c] = 2; |
| 76 | + |
| 77 | + } |
| 78 | + |
| 79 | + void gameOfLife(vector<vector<int>>& board) { |
| 80 | + for (int i=0; i<board.size(); i++) { |
| 81 | + for (int j=0; j<board[0].size(); j++) { |
| 82 | + liveCheck(board, i, j); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + for (int i=0; i<board.size(); i++) { |
| 87 | + for (int j=0; j<board[0].size(); j++) { |
| 88 | + board[i][j] >>= 1; |
| 89 | + } |
| 90 | + |
| 91 | + } |
| 92 | + |
| 93 | + } |
| 94 | +}; |
0 commit comments