|
| 1 | +/** |
| 2 | + * [289] Game of Life |
| 3 | + * |
| 4 | + * According to the <a href="https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life" target="_blank">Wikipedia's article</a>: "The Game of Life, also known simply as Life, is a cellular automaton devised by the British mathematician John Horton Conway in 1970." |
| 5 | + * |
| 6 | + * Given a board with m by n cells, each cell has an initial state live (1) or dead (0). Each cell interacts with its <a href="https://en.wikipedia.org/wiki/Moore_neighborhood" target="_blank">eight neighbors</a> (horizontal, vertical, diagonal) using the following four rules (taken from the above Wikipedia article): |
| 7 | + * |
| 8 | + * <ol> |
| 9 | + * Any live cell with fewer than two live neighbors dies, as if caused by under-population. |
| 10 | + * Any live cell with two or three live neighbors lives on to the next generation. |
| 11 | + * Any live cell with more than three live neighbors dies, as if by over-population.. |
| 12 | + * Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction. |
| 13 | + * </ol> |
| 14 | + * |
| 15 | + * Write a function to compute the next state (after one update) of the board given its current state. <span>The next state is created by applying the above rules simultaneously to every cell in the current state, where births and deaths occur simultaneously.</span> |
| 16 | + * |
| 17 | + * Example: |
| 18 | + * |
| 19 | + * |
| 20 | + * Input: |
| 21 | + * <span id="example-input-1-1">[ |
| 22 | + * [0,1,0], |
| 23 | + * [0,0,1], |
| 24 | + * [1,1,1], |
| 25 | + * [0,0,0] |
| 26 | + * ]</span> |
| 27 | + * Output: |
| 28 | + * <span id="example-output-1">[ |
| 29 | + * [0,0,0], |
| 30 | + * [1,0,1], |
| 31 | + * [0,1,1], |
| 32 | + * [0,1,0] |
| 33 | + * ]</span> |
| 34 | + * |
| 35 | + * |
| 36 | + * Follow up: |
| 37 | + * |
| 38 | + * <ol> |
| 39 | + * 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. |
| 40 | + * 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? |
| 41 | + * </ol> |
| 42 | + * |
| 43 | + */ |
| 44 | +pub struct Solution {} |
| 45 | + |
| 46 | +// submission codes start here |
| 47 | + |
| 48 | +// in-place: 1: live->live, 0: die->die, 2: die->live, 3: live->die |
| 49 | +impl Solution { |
| 50 | + pub fn game_of_life(board: &mut Vec<Vec<i32>>) { |
| 51 | + let (height, width) = (board.len(), board[0].len()); |
| 52 | + let neighbors = vec![(-1,-1),(-1,0),(-1,1),(0,-1),(0,1),(1,-1),(1,0),(1,1)]; |
| 53 | + for i in 0..height { |
| 54 | + for j in 0..width { |
| 55 | + let mut live = 0; |
| 56 | + for offset in neighbors.iter() { |
| 57 | + if (offset.0 < 0 && i == 0) || |
| 58 | + (offset.0 > 0 && i == height -1) || |
| 59 | + (offset.1 < 0 && j == 0) || |
| 60 | + (offset.1 > 0 && j == width -1) { |
| 61 | + continue |
| 62 | + } |
| 63 | + let v = board[(i as i32 + offset.0) as usize][(j as i32 + offset.1) as usize]; |
| 64 | + if v == 1 || v == 3 { |
| 65 | + live += 1; |
| 66 | + } |
| 67 | + } |
| 68 | + if board[i][j] == 1 && (live < 2 || live > 3) { |
| 69 | + // go die |
| 70 | + board[i][j] = 3; |
| 71 | + } else if board[i][j] == 0 && live == 3 { |
| 72 | + // go live |
| 73 | + board[i][j] = 2; |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + for i in 0..height { |
| 79 | + for j in 0..width { |
| 80 | + if board[i][j] == 2 { |
| 81 | + board[i][j] = 1; |
| 82 | + } else if board[i][j] == 3 { |
| 83 | + board[i][j] = 0; |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +// submission codes end |
| 91 | + |
| 92 | +#[cfg(test)] |
| 93 | +mod tests { |
| 94 | + use super::*; |
| 95 | + |
| 96 | + #[test] |
| 97 | + fn test_289() { |
| 98 | + let mut test = vec![ |
| 99 | + vec![0,1,0], |
| 100 | + vec![0,0,1], |
| 101 | + vec![1,1,1], |
| 102 | + vec![0,0,0], |
| 103 | + ]; |
| 104 | + Solution::game_of_life(&mut test); |
| 105 | + assert_eq!(test, vec![ |
| 106 | + vec![0,0,0], |
| 107 | + vec![1,0,1], |
| 108 | + vec![0,1,1], |
| 109 | + vec![0,1,0], |
| 110 | + ]); |
| 111 | + } |
| 112 | +} |
0 commit comments