UVALive 4034

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

Description

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 i into a black stone, otherwise, turn it to a white stone. The checking is based on the previous iterated game board configuration, and NOT 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 3m512 .
  2. The number of iterations is t , where 1t100 .

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

//模拟题
#include
<iostream> #include <stdio.h> #include <string.h> #include <algorithm> #include <queue> #include <cmath> using namespace std; char map[523][523]; int rc[10000][2]; int C; bool del(int &i,int &j) { int x,y; int ct=0; for(x=i-1;x<=i+1;x++) for(y=j-1;y<=j+1;y++) if(map[x][y]=='b') ct++; if(map[i][j]=='b') { if(ct<5) { return 1; } } else { if(ct>4) return 1; } return 0; } int main() { int T; int m,t; int i,j,k; scanf("%d",&T); while(T--) { scanf("%d%d",&m,&t); for(i=1;i<=m;i++) scanf("%s",map[i]+1); while(t--) { C=0; for(i=2;i<m;i++) for(j=2;j<m;j++) if(del(i,j)) { rc[C][0]=i; rc[C][1]=j; C++; } for(i=0;i<C;i++) { if(map[rc[i][0]][rc[i][1]]=='b') map[rc[i][0]][rc[i][1]]='w'; else map[rc[i][0]][rc[i][1]]='b'; } } k=0; for(i=1;i<=m;i++) for(j=1;j<=m;j++) if(map[i][j]=='b') k++; printf("%d %d\n",k,m*m-k); } return 0; }

你可能感兴趣的:(live)