Game of Life

Game of Life
Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

Description

Download as PDF

LIFE is a evolutionary game played on a 2D game board. Initially, the game board is filled with white and black stones. For each iteration of the game, each stone is checked and perhaps changed to the other color according to the rules given below:

  1. For each stone i , if there are more black stones than white stones in the 3×3 neightborhood centered at stone i , then turn stone iinto a black stone, otherwise, turn it to a white stone. The checking is based on the previous iterated game board configuration, andNOT the current iteration of intermediate game board. In other words, the check of all stones are done simultaneously. Thus, changing the stone color will not affect any other stone in the same iteration.
  2. The boundary of the game board will remain unchanged throughout all iterations. In other words, there is no need to check the stones located on the boundary of the game board.

Please write a program that when given an initial game board configuration and the number of iterations of LIFE, compute and output the number of black and white stones on the resulting game board.


Technical Specification

  1. The game board size is m×m where 3$ \le$m$ \le$512 .
  2. The number of iterations is t , where 1$ \le$t$ \le$100 .

Input

The first line of the input contains an integer n indicating the number of test cases to follow. For each test case, the first line contains two integers, m and t , specifying the game board dimension and number of LIFE iterations. The next m lines outline the configuration of the initial m×m game board. Each line contains m consecutive characters, where each character is either ``b" or ``w" denoting black stone or white stone, respectively.

Output

For each test case, output on a single line the number of black stones and white stones of the resulting game board.


Note: The final game board configuration for the below two cases are:

wbwb 
bbbw 
wbbb 
wbwb 

wbbbb 
wwbww 
wwwww 
wwwww 
wwwww

Sample Input

2 
4 1 
wbwb 
bbbw 
wwbb 
wbwb 
5 1 
wbbbb 
wbwbw 
wwwww 
wwwww 
wwwww

Sample Output

10 6 
5 20

思路:其实这题不难,关键是状态的储存。我设置了map1和map2用于储存当前状态和下一状态,在函数check(见以下代码)中,当map1储存当前状态时,map2就是下一状态,反之亦然。另外一个关键点是怎么判断3×3区域内黑多还是白多,我设置了一个值BLACK = 5*'b'+4*'w',这是黑多于白的临界情况,由于' b ' < ' w ',所以当3×3区域内所有字符的ASC2码相加的结果大于BLACK时,就是白多,反之黑多。


AC Code:

#include <stdio.h>
#include <string.h>
#define BLACK 5*'b'+4*'w'

char map1[513][513], map2[513][513];
int flag, b;
int m, t;

void check(int i, int j)
{
    if(flag > 0)
    {
        if(map1[i-1][j-1] + map1[i][j-1] + map1[i+1][j-1] + map1[i-1][j] + map1[i+1][j] + map1[i-1][j+1] + map1[i][j+1] + map1[i+1][j+1] + map1[i][j] > BLACK)
        {
            /*1.当且仅当当前状态为b时才执行b--。2.注意map2[i][j] = 'w'不能放在if中
            因为无论if中的条件是否为真,此指令都需要执行,因为map1[i][j] == 'w'(if
            判断条件为假)时不意味着map2[i][j] == 'w'。如果不理解可以把程序后附上的
            例子执行一次*/
            if(map1[i][j] == 'b')
                b--;
            map2[i][j] = 'w';
        }
        else
        {
            if(map1[i][j] == 'w')
                b++;
            map2[i][j] = 'b';
        }
    }
    else
    {
        if(map2[i-1][j-1] + map2[i][j-1] + map2[i+1][j-1] + map2[i-1][j] + map2[i+1][j] + map2[i-1][j+1] + map2[i][j+1] + map2[i+1][j+1] + map2[i][j] > BLACK)
        {
            if(map2[i][j] == 'b')
                b--;
            map1[i][j] = 'w';

        }
        else
        {
            if(map2[i][j] == 'w')
                b++;
            map1[i][j] = 'b';
        }
    }
}

int main()
{
    int n;
    scanf("%d", &n);
    while(n--)
    {
        scanf("%d%d", &m, &t);
        for(int i = 0; i < m; i++)
        {
            scanf("%s", map1[i]);
            strcpy(map2[i], map1[i]);
        }
        //for(int i=0; i<m; i++) puts(map2[i]);
        flag = 1; b = 0;
        for(int i = 0; i < m; i++)
        {
            for(int j = 0; j < m; j++)
                if(map1[i][j] == 'b')
                    b++;
        }
        //printf("%d %d", b, w);
        while(t--)
        {
            for(int i = 1; i < m-1; i++)
            {
                for(int j = 1; j< m-1; j++)
                    check(i, j);
            }
            flag = -flag;
        }
        //for(int i = 0; i < m; i++) puts(map2[i]);
        printf("%d %d\n", b, m * m - b);
    }
    return 0;
}

3 3
www
wdw
www

原文地址:https://www.cnblogs.com/cszlg/p/2910577.html