zoukankan      html  css  js  c++  java
  • 2017年第八蓝桥杯C/C++ A组国赛 —— 第二题:生命游戏

    标题:生命游戏

    康威生命游戏是英国数学家约翰·何顿·康威在1970年发明的细胞自动机。
    这个游戏在一个无限大的2D网格上进行。

    初始时,每个小方格中居住着一个活着或死了的细胞。
    下一时刻每个细胞的状态都由它周围八个格子的细胞状态决定。

    具体来说:

    1. 当前细胞为存活状态时,当周围低于2个(不包含2个)存活细胞时, 该细胞变成死亡状态。(模拟生命数量稀少)
    2. 当前细胞为存活状态时,当周围有2个或3个存活细胞时, 该细胞保持原样。
    3. 当前细胞为存活状态时,当周围有3个以上的存活细胞时,该细胞变成死亡状态。(模拟生命数量过多)
    4. 当前细胞为死亡状态时,当周围有3个存活细胞时,该细胞变成存活状态。 (模拟繁殖)

    当前代所有细胞同时被以上规则处理后, 可以得到下一代细胞图。按规则继续处理这一代的细胞图,可以得到再下一代的细胞图,周而复始。

    例如假设初始是:(X代表活细胞,.代表死细胞)

    .....
    .....
    .XXX.
    .....
    
    

    下一代会变为:

    .....
    ..X..
    ..X..
    ..X..
    .....
    

    康威生命游戏中会出现一些有趣的模式。例如稳定不变的模式:

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

    还有会循环的模式:

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

    本题中我们要讨论的是一个非常特殊的模式,被称作"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.......................
    ......................................
    

    假设以上初始状态是第0代,请问第1000000000(十亿)代一共有多少活着的细胞?

    注意:我们假定细胞机在无限的2D网格上推演,并非只有题目中画出的那点空间。
    当然,对于遥远的位置,其初始状态一概为死细胞。

    注意:需要提交的是一个整数,不要填写多余内容。

    Code

    /*
                                    ^....0
                                   ^ .1 ^1^
                                   ..     01
                                  1.^     1.0
                                 ^ 1  ^    ^0.1
                                 1 ^        ^..^
                                 0.           ^ 0^
                                 .0            1 .^
                                 .1             ^0 .........001^
                                 .1               1. .111100....01^
                                 00                 11^        ^1. .1^
                                 1.^                              ^0  0^
                                   .^                                 ^0..1
                                   .1                                   1..^
                                 1 .0                                     ^  ^
                                  00.                                     ^^0.^
                                  ^ 0                                     ^^110.^
                              0   0 ^                                     ^^^10.01
                       ^^     10  1 1                                      ^^^1110.1
                       01     10  1.1                                      ^^^1111110
                       010    01  ^^                                        ^^^1111^1.^           ^^^
                       10  10^ 0^ 1                                            ^^111^^^0.1^       1....^
                        11     0                                               ^^11^^^ 0..  ....1^   ^ ^
                        1.     0^                                               ^11^^^ ^ 1 111^     ^ 0.
                       10   00 11                                               ^^^^^   1 0           1.
                       0^  ^0  ^0                                                ^^^^    0            0.
                       0^  1.0  .^                                               ^^^^    1 1          .0
                       ^.^  ^^  0^                             ^1                ^^^^     0.         ^.1
                       1 ^      11                             1.                ^^^     ^ ^        ..^
                      ^..^      ^1                             ^.^               ^^^       .0       ^.0
                      0..^      ^0                              01               ^^^       ..      0..^
                     1 ..        .1                             ^.^              ^^^       1 ^  ^0001
                    ^  1.        00                              0.             ^^^        ^.0 ^.1
                    . 0^.        ^.^                             ^.^            ^^^         ..0.0
                   1 .^^.         .^                  1001        ^^            ^^^         . 1^
                   . ^ ^.         11                0.    1         ^           ^^          0.
                    0  ^.          0              ^0       1                   ^^^          0.
                  0.^  1.          0^             0       .1                   ^^^          ..
                  .1   1.          00            .        .1                  ^^^           ..
                 1      1.         ^.           0         .^                  ^^            ..
                 0.     1.          .^          .         0                                  .
                 .1     1.          01          .        .                                 ^ 0
                ^.^     00          ^0          1.       ^                                 1 1
                .0      00           .            ^^^^^^                                   .
                .^      00           01                                                    ..
               1.       00           10                                                   1 ^
              ^.1       00           ^.                                            ^^^    .1
              ..        00            .1                                        1..01    ..
             1.1         00           1.                                       ..^      10
            ^ 1^         00           ^.1                                      0 1      1
            .1           00            00                                       ^  1   ^
             .           00            ^.^                                        10^  ^^
           1.1           00             00                                              10^
           ..^           1.             ^.                                               1.
          0 1            ^.              00                 00                            .^
            ^            ^.              ^ 1                00   ^0000^     ^               01
         1 0             ^.               00.0^              ^00000   1.00.1              11
         . 1              0               1^^0.01                      ^^^                01
          .^              ^                1   1^^                                       ^.^
        1 1                                                                              0.
        ..                                                                              1 ^
         1                                                                               1
       ^ ^                                                                             .0
       1                                                                             ^ 1
       ..                                                          1.1            ^0.0
      ^ 0                                                           1..01^^100000..0^
      1 1                                                            ^ 1 ^^1111^ ^^
      0 ^                                                             ^ 1      1000^
      .1                                                               ^.^     .   00
      ..                                                                1.1    0.   0
      1.                                                                  .    1.   .^
      1.                                                                 1    1.   ^0
     ^ .                                                                 ^.1 00    01
     ^.0                                                                  001.     .^
     */
    /* Procedural objectives:
    
     Variables required by the program:
    
     Procedural thinking:
    
     Functions required by the program:
    
     Determination algorithm:
    
     Determining data structure:
    
    
    */
    /* My dear Max said:
    "I like you,
    So the first bunch of sunshine I saw in the morning is you,
    The first gentle breeze that passed through my ear is you,
    The first star I see is also you.
    The world I see is all your shadow."
    
    FIGHTING FOR OUR FUTURE!!!
    */
    #include <map>
    #include <iostream>
    
    #define MAP map<Node,bool>
    
    using namespace std;
    
    int Left=0,Top=0,Right=40,Bottom=15;
    int index=1,count=36;
    
    typedef struct Node{
    	int x,y;
    	Node(int x,int y):x(x),y(y){
    	}
    	bool operator < (const Node& o) const{
    		if(x!=o.x) return x<o.x;
    		return y<o.y;
    	}
    }Node;
    
    MAP pre,now;
    
    int refresh_bound(int x,int y){
    	Top=min(Top,x);
    	Bottom=max(Bottom,x);
    	Left=min(Left,y);
    	Right=max(Right,y);
    }
    
    int get_neighbor(MAP &mp,int x,int y){
    	int ans=0;
    	for(int i=-1;i<2;i++)
    		for(int j=-1;j<2;j++)
    			if((i||j)&&(mp[Node(x+i,y+j)]))
    				ans++;
    	return ans;
    }
    
    void epoch(){
    	pre=now;
    	now=MAP();
    	
    	int tmp_count=0;
    	for(int i=Top-1;i<Bottom+1;i++){
    		for(int j=Left-1;j<Right+1;j++){
    			int nums=get_neighbor(pre,i,j);
    			if(pre[Node(i,j)]){		//活细胞 
    				if(nums>1&&nums<4){		//周围细胞适合 
    					now[Node(i,j)]=1;
    					tmp_count++;
    				}
    			}else{		//死细胞 
    				if(nums==3){	//繁殖 
    					now[Node(i,j)]=1;
    					refresh_bound(i,j);
    					tmp_count++;
    				}
    			}
    		}
    	}
    	cout<<index<<' '<<tmp_count<<' '<<tmp_count-count<<endl;
    	count=tmp_count;
    	index++;
    }
    
    void display(MAP &mp){
    	for(int i=Top;i<Bottom+1;i++){
    		for(int j=Left;j<Right+1;j++){
    			if(mp[Node(i,j)]) putchar('X');
    			else putchar(' ');
    		}
    		puts("");
    	} 
    	puts("");
    }
    
    int main(){
    	freopen("input.txt","r",stdin);
    	freopen("output.txt","w",stdout);
    	
    	char buf[100];
    	for(int i=0;i<Bottom+1;i++){
    		scanf("%s",buf);
    		for(int j=0;j<Right+1;j++){
    			if(buf[j]=='X'){
    				now[Node(i,j)]=1;
    			}
    		}
    	}
    	for(int i=0;i<300;i++)
    		epoch();
    	return 0;
    }
    
  • 相关阅读:
    [NOI2015]程序自动分析
    D-query(莫队)
    小B的询问
    组合的输出 (dfs+记忆化)
    组合的输出 (dfs+记忆化)
    5719: 集合的划分(dfs)
    Search for a range, 在一个可能有重复元素的有序序列里找到指定元素的起始和结束位置
    Find Min In Rotated Sorted Array2,包含重复数字的反转序列找最小值。
    Find Min In Rotated Sorted Array,寻找反转序列中最小的元素。
    Search In Rotated SortedArray2, 有重复数据的反转序列。例如13111.
  • 原文地址:https://www.cnblogs.com/AlexKing007/p/12338266.html
Copyright © 2011-2022 走看看