zoukankan      html  css  js  c++  java
  • Game of Life

    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):

    1. Any live cell with fewer than two live neighbors dies, as if caused by under-population.
    2. Any live cell with two or three live neighbors lives on to the next generation.
    3. Any live cell with more than three live neighbors dies, as if by over-population..
    4. 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: 

    1. 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.
    2. 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?

    Analyse: The problem is how we keep the original state and the updated state. We can use four number to represent four states:

    dead_dead: 0

    live_dead: 1

    dead_live: 2

    live_live: 3

    Note that we'd better to use obvious number to stand for the original "live" state and the updated "live" state. 

    Runtime: 0ms

     1 class Solution {
     2 private:
     3     void live(vector<vector<int> > &board, int i, int j){
     4         int neighbor = 0;
     5         for(int m = i - 1; m <= i + 1; m++){
     6             for(int n = j - 1; n <= j + 1; n++){
     7                 if(m < 0 || m >= board.size() || n < 0 || n >= board[0].size())
     8                     continue;
     9                 else if(m == i && n == j)
    10                     continue;
    11                 else{
    12                     if(board[m][n] % 2)
    13                         neighbor++; //calculate how many live neighbors
    14                 }
    15             }
    16         }
    17         if(board[i][j]){
    18             if(neighbor < 2 || neighbor > 3)
    19                 board[i][j] = 1;
    20             else 
    21                 board[i][j] = 3;
    22         }        
    23         else{
    24             if(neighbor == 3)
    25                 board[i][j] = 2;
    26             else
    27                 board[i][j] = 0;
    28         }
    29     }
    30 public:
    31     void gameOfLife(vector<vector<int>>& board) {
    32         if(board.empty() || board[0].empty()) return;
    33         int m = board.size(), n = board[0].size();
    34         
    35         int dead_dead = 0, live_dead = 1;
    36         int dead_live = 2, live_live = 3;
    37         for(int i = 0; i < m; i++){
    38             for(int j = 0; j < n; j++){
    39                  live(board, i, j);
    40             }
    41         }
    42         for(int i = 0; i < m; i++){
    43             for(int j = 0; j < n; j++){
    44                 board[i][j] = board[i][j] < 2 ? 0 : 1;
    45             }
    46         }
    47     }
    48 };
  • 相关阅读:
    派生
    什么是类的继承
    python中一切皆对象
    类之属性查找
    类之 __init__方法

    MySql cmd下的学习笔记 —— 有关分组的操作(group by)
    MySql cmd下的学习笔记 —— 有关select的操作(max, min等常见函数)
    MySql cmd下的学习笔记 —— 有关select的操作(in, and, where, like等等)
    MySql cmd下的学习笔记 —— 有关表的操作(对表的增删改查)
  • 原文地址:https://www.cnblogs.com/amazingzoe/p/4941502.html
Copyright © 2011-2022 走看看