Game of Life (4.2leetcode daily punch)

According to Baidu Encyclopedia, Game of Life, referred to as life, is a British mathematician John Horton Conway cellular automata 1970 invention.
Given a, m × n grid panels, each grid can be seen as a cell. Each cell having an initial state: 1 is the viable cells (live), that is, 0, or dead cells (dead). Per cell to its eight neighboring positions (horizontal, vertical, diagonal) are the following four cell survival Law:

 If the number of viable cells of eight positions around fewer than two live cells, the location of a live cell death;
 if eight positions around the living cell has two or three living cells, the location of living cells were still alive;
 if living cells there are more than eight positions around the three living cells, the location of a live cell death;
 If you happen to have three dead cells around the living cell, the position of the dead cells resurrection;

According to the current state, a write function to calculate the next state (after update) on all the cells of the panel. The next state is formed by the above rules simultaneously applied to each cell in the current state, wherein the birth and death of cells occurs simultaneously.
 
Example:
Input:
[
  [0,1,0],
  [0,0,1],
  [1,1,1],
  [0,0,0]
]
Output:
[
  [0,0,0],
  [0 , 1],
  [0,1,1],
  [0,1,0]
]
 
Thinking: traversing element around each element in eight directions, is determined, the simulation.
However, this question can be learned by two array:  int [] = DX new new int [] { 0 , 0 , . 1 , - . 1 , . 1 , - . 1 , - . 1 , . 1 }; and  int [] = Dy new new int [] { . 1 , - . 1 , 0 , 0 , . 1 , . 1 , - . 1 , - . 1 }; and then there is a loop from a direction traversing 8 0,  int newX = I + DX [K]; 
Int newY J = Dy + [K]; this traversal eight directions.
 1 void gameOfLife(int** board, int boardSize, int* boardColSize) {
 2     if (boardColSize[0] == 0)
 3         return;
 4     int a[boardSize][boardColSize[0]];
 5     int dir[8][2] = { {1,0},{0,1},{-1,0},{0,-1},{-1,-1},{1,1},{-1,1},{1,-1} };
 6     int i = 0, j = 0, z;
 7     for (i = 0; i < boardSize; i++)
 8     {
 9         for (j = 0; j < boardColSize[0]; j++)
10         {
11             a[i][j] = 0;
12         }
13     }
14 
15 
16     for (i = 0; i < boardSize; i++)
17     {
18         for (j = 0; j < boardColSize[i]; j++)
19         {
20             int k = 0;
21             for (z = 0; z < 8; z++)
22             {
23                 int x = i + dir[z][0];
24                 int y = j + dir[z][1];
25                 if (x < 0 || y < 0 || x >= boardSize || y >= boardColSize[0])
26                 {
27                     continue;
28                 }
29                 if (board[x][y] == 1)
30                     k++;
31             }
32             if (k < 2)
33                 a[i][j] = 0;
34             if (board[i][j] == 1 && k >= 2 && k <= 3)
35                 a[i][j] = 1;
36             if (board[i][j] == 1 && k > 3)
37                 a[i][j] = 0;
38             if (k == 3)
39                 a[i][j] = 1;
40         }
41     }
42     for (i = 0; i < boardSize; i++)
43     {
44         for (j = 0; j < boardColSize[0]; j++)
45         {
46             board[i][j] = a[i][j];
47         }
48     }
49 }

 

 

Guess you like

Origin www.cnblogs.com/ZhengLijie/p/12617694.html