A. Game of Life

文章讲述了威廉开发一个简化版的GameofLife游戏,定义在包含n个细胞的数组上,描述了数组的进化规则和输入输出格式。程序代码展示了如何根据邻居数量和初始状态进行迭代计算,以求解给定初始状态数组经过m次迭代后的状态。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

1.Problem

2.Input

3.Output

4.Examples

4.1input

4.2output

5.Code

6.Conclusion


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次,最终输出结果。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

向阳而生__

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值