目录
1.Problem
William really likes the cellular automaton called "Game of Life" so he decided to make his own version. For simplicity, William decided to define his cellular automaton on an array containing nn cells, with each cell either being alive or dead.
Evolution of the array in William's cellular automaton occurs iteratively in the following way:
- If the element is dead and it has exactly 11 alive neighbor in the current state of the array, then on the next iteration it will become alive. For an element at index ii the neighbors would be elements with indices i−1i−1 and i+1i+1. If there is no element at that index, it is considered to be a dead neighbor.
- William is a humane person so all alive elements stay alive.
Check the note section for examples of the evolution.
You are given some initial state of all elements and you need to help William find the state of the array after mm iterations of evolution.
2.Input
Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1031≤t≤103). Description of the test cases follows.
The first line of each test case contains two integers nn and mm (2≤n≤103,1≤m≤1092≤n≤103,1≤m≤109), which are the total number of cells in the array and the number of iterations.
The second line of each test case contains a string of length nn made up of characters "0" and "1" and defines the initial state of the array. "1" means a cell is alive and "0" means it is dead.
It is guaranteed that the sum of nn over all test cases does not exceed 104104.
3.Output
In each test case output a string of length nn, made up of characters "0" and "1" — the state of the array after mm iterations of evolution.
4.Examples
4.1input
4
11 3
01000000001
10 2
0110100101
5 2
10101
3 100
000
4.2output
11111001111
1110111101
10101
000
5.Code
#include<bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
template <typename T> bool chkmax(T &x,T y){return x<y?x=y,true:false;}
template <typename T> bool chkmin(T &x,T y){return x>y?x=y,true:false;}
int readint(){
int x=0,f=1; char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
int n,m;
char s[1005];
int main(){
int T=readint();
while(T--){
n=readint(); m=readint();
chkmin(m,n);
scanf("%s",s+1);
s[0]=s[n+1]='0'; // Extend the boundary with '0' for boundary checks
for(int i=0;i<m;i++){
char prev = '0'; // Store the previous character
for(int j=1;j<=n;j++){
char temp = s[j]; // Store the current character
if (prev != temp) {
s[j] = '1';
} else {
s[j] = '0';
}
prev = temp; // Update the previous character for the next iteration
}
}
puts(s+1);
}
return 0;
}
6.Conclusion
1.引入了一些必要的C++标准库头文件,以及定义了一些宏(macros),如pb、mp、fi、se,以及C++中常见的类型别名(typedef)如ll(long long)、ull(unsigned long long)、pii(pair of int,一对整数的类型别名)和pll(pair of ll,一对长整数的类型别名)。
2.定义了两个模板函数chkmax和chkmin,它们用于更新传入的变量,如果条件满足则更新。这些函数返回一个布尔值,表示是否进行了更新。
3.定义了一个函数readint,用于从标准输入中读取一个整数。
4.声明了一些全局变量:n、m和字符数组char s[1005]。
5.进入了程序的main函数。程序首先读取一个整数T,表示测试用例的数量。
6.接下来,在一个循环中,针对每个测试用例进行以下操作:读取两个整数n和m,分别表示字符串的长度和迭代次数。
调用chkmin函数,比较n和m,并取较小的值,然后将结果赋给m。
使用scanf从标准输入读取一个二进制字符串,并将其存储在字符数组s的第二个位置(从1开始,因为s[0]和s[n+1]被初始化为'0'用于边界检查)。
接下来,执行一个循环,重复m次,用于更新字符串。
在每次迭代中,它遍历字符串s中的字符,根据相邻字符的值来更新当前字符的值,然后将结果存储回s中。
最后,使用puts函数打印出更新后的字符串,注意输出时从s的第二个位置开始,因为第一个位置是辅助的'0'字符。总体来说,这个程序的目的是对输入的二进制字符串进行一系列迭代操作,并输出最终结果。在每次迭代中,它根据相邻字符的值更新当前字符的值,然后重复这个过程m次,最终输出结果。