2017 8th Blue Bridge Cup Java Programming Undergraduate Group B Final Game of Life (Fill in the blank)

2017 8th Blue Bridge Cup Java Programming Undergraduate Group B Final Individual Problem Solution Summary:

https://blog.csdn.net/daixinliangwyx/article/details/90184941

 

Question 2

Title: Game of Life

Conway's Game of Life is a cellular automaton invented by British mathematician John Horton Conway in 1970.  
The game is played on an infinite 2D grid.


Initially, each small square is populated by a living or dead cell.
The state of each cell at the next moment is determined by the state of the cells in the eight grids around it.


Specifically:


1. When the current cell is in a viable state, when there are less than 2 (excluding 2) viable cells around, the cell becomes a dead state. (The number of simulated life is scarce)
2. When the current cell is alive, when there are 2 or 3 living cells around, the cell remains the same.
3. When the current cell is in a viable state, when there are more than 3 viable cells around, the cell becomes a dead state. (The number of simulated lives is too much)
4. When the current cell is in a dead state, when there are 3 surviving cells around, the cell becomes a living state. (simulated breeding)


After all cells of the current generation are processed by the above rules at the same time, the next generation cell map can be obtained. Continue to process the cell map of this generation according to the rules, and you can obtain the cell map of the next generation, and repeat.


For example, suppose the initial is: (X represents live cells, . represents dead cells)
.....
.....
.XXX
... .....


The next generation will become:
.....
..X..
..X..
..X
..

There will be some interesting modes in Conway's Game of Life. For example a stable pattern:

....
.XX.
.XX.
....

There is also a looping mode:

......      ......       ......
.XX...      .XX...       .XX...
.XX...      .X....       .XX...
...XX.   -> ....X.  ->   ...XX.
...XX.      ...XX.       ...XX.
......      ......       ......


In this question we are going to discuss a very special mode called "Gosper glider gun":


......................................
.........................X............
.......................X.X............
.............XX......XX............XX.
............X...X....XX............XX.
.XX........X.....X...XX...............
.XX........X...X.XX....X.X............
...........X.....X.......X............
............X...X.....................
.............XX.......................
......................................

Assuming the above initial state is generation 0, how many living cells are there in the 1000000000th (billion) generation?

Note: We assume that the cellular machine operates on an infinite 2D grid, not just the space drawn in the title.
Of course, for distant locations, the initial state is always dead cells.


Note: What needs to be submitted is an integer, do not fill in redundant content.


Solution: The title requires one billion generations, so it is definitely not possible to directly simulate each generation. After all, the picture cannot be opened so large, so consider whether there is some kind of law in reproduction. First expand the graph of the initial state a little so that the initial state is in the middle of the whole graph, which is convenient for subsequent reproduction and diffusion, first simulate some algebraic reproduction, observe the difference between the number of surviving cells in each generation and the previous generation, and output the results to a txt file , it can be found that the differences of the previous generations (starting from the first generation) are 3, 4, 5, 3, -7, 7..., and the differences from the 31st generation are 3, 4, 5, 3, - 7, 7..., the difference from the 61st generation is 3, 4, 5, 3, -7, 7..., so it can be known that the difference in cell reproduction repeats every 30 generations, and every After 30 generations of propagation, the number of surviving cells increases by 5, so it is possible to directly calculate the number of surviving cells after a billion generations.

Code to simulate 100 generations of reproduction:

#include<bits/stdc++.h>
using namespace std;
char Map[50][100], next[50][100];
int len, n = 50, m = 100, sum, presum;
string s;
int dir[8][2] = {-1, -1, -1, 0, -1, 1, 0, -1, 0, 1, 1, -1, 1, 0, 1, 1};
void dfs(int x, int y) {
  sum = 0;
  if(x == n && y == 0) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
//        cout << next[i][j];
        if (next[i][j] == 'X') sum++;
      }
//      cout << endl;
    }
    cout << "存活细胞数量: " << sum << "  比上一代繁殖了 " << sum-presum << endl;
    presum = sum;
    return;
  }
  int num = 0;
  for(int i = 0; i < 8; i++) {
    int nextx = x + dir[i][0];
    int nexty = y + dir[i][1];
    if (nextx >= 0 && nextx < n && nexty >= 0 && nexty < m && Map[nextx][nexty] == 'X') num++;
  }
  if (Map[x][y] == '.' && num == 3) next[x][y] = 'X';
  else if (Map[x][y] == 'X' && (num < 2 || num > 3)) next[x][y] = '.';
  else
    next[x][y] = Map[x][y];
  if (y+1 >= m) dfs(x+1, 0);
  else
    dfs(x, y+1);
}
int main() {
  freopen("F:\\out.txt", "w", stdout); //输出重定向,输出数据将保存在F盘中的out.txt文件中
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      Map[i][j] = '.';
  sum = 0;
  for (int i = 11; i < 22; i++) {
    getline(cin, s);
    len = s.size();
    for (int j = len; j < len*2; j++) {
      Map[i][j] = s[j-len];
      if (Map[i][j] == 'X') sum++;
    }
  }
  cout << "第0代:";
//  for (int i = 0; i < n; i++) {
//    for (int j = 0; j < m; j++)
//      cout << Map[i][j];
//    cout << endl;
//  }
  cout << "存活细胞数量: " << sum << "  开始繁殖 " << endl;
  presum = sum;
  for (int i = 1; i < 100; i++) {
    cout << "第" << i << "代: ";
    dfs(0, 0);
    for (int ii = 0; ii < n; ii++)
      for (int jj = 0; jj < m; jj++)
        Map[ii][jj] = next[ii][jj];
  }
  fclose(stdout);//关闭输出重定向 
  return 0;
}

Calculate the answer:

#include<bits/stdc++.h>
using namespace std;
long long add[31] = {0, 3, 4, 5, 3, -7, 7, -3, 13, -19, 6, 2, 4, 1, 1, -14, 2, 3, 6, 1, 0, 0, -5, 11, -17, 7, -3, 0, 3, -2, -7};
int main() {
  long long ans = 36;
  ans += 1000000000 / 30 * 5;//先算有多少个30代,每代增加5个也就是再*5,千万不能先乘再除因为这样会算错代数 
  for (long long i = 1; i <= 1000000000%30; i++)
    ans += add[i];
  cout << ans << endl;
  return 0;
}

Answer: 166666713

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326527603&siteId=291194637