cf1523A. Game of Life
题意:
包含n个元素的数组,数值为1或0,如果一个元素为0,并且其周围正好只有一个为1的元素,那么下一刻本元素也会变成1.
给你一个数值,问你m次时刻后数组的状态
题解:
注意,101情况下,中间的0不能变成1,因为题目说的是周围只有一个1,如果有两个1就不行了。对于每一位i,我们用j表示偏移量,相当于左移j位,右移j位,看i-j位和i+j位是否满足是由一个是1,如果满足就变成1
代码:
// Problem: A. Game of Life
// Contest: Codeforces - Deltix Round, Spring 2021 (open for everyone, rated, Div. 1 + Div. 2)
// URL: https://codeforces.com/contest/1523/problem/A
// Memory Limit: 256 MB
// Time Limit: 1000 ms
//#include <bits/stdc++.h>
#include <unordered_map>
#define debug(a, b) printf("%s = %d\n", a, b);
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
clock_t startTime, endTime;
//Fe~Jozky
const ll INF_ll= 1e18;
const int INF_int= 0x3f3f3f3f;
template <typename T> inline void read(T& x)
{T f= 1;x= 0;char ch= getchar();while (0 == isdigit(ch)) {if (ch == '-')f= -1;ch= getchar();}while (0 != isdigit(ch))x= (x << 1) + (x << 3) + ch - '0', ch= getchar();x*= f;
}
template <typename T> inline void write(T x)
{if (x < 0) {x= ~(x - 1);putchar('-');}if (x > 9)write(x / 10);putchar(x % 10 + '0');
}
void rd_test()
{
#ifdef ONLINE_JUDGE
#elsestartTime= clock();freopen("in.txt", "r", stdin);
#endif
}
void Time_test()
{
#ifdef ONLINE_JUDGE
#elseendTime= clock();printf("\nRun Time:%lfs\n", (double)(endTime - startTime) / CLOCKS_PER_SEC);
#endif
}
int main()
{//rd_test();string a, b;long long n, m, i, t, j;cin >> t;for (t= t; t > 0; t--) {cin >> n >> m >> a;b= a;for (i= 0; i < n; i++) {j= 0;while ((i - j >= 0 || i + j < n) && (i - j < 0 || a[i - j] == '0') && (i + j >= n || a[i + j] == '0'))j++;if (i - j >= 0) {if (a[i - j] == '1' && (i + j >= n || a[i + j] == '0') && j <= m) //在此步剔除101的情况{b[i]= '1';}}if (i + j < n) {if (a[i + j] == '1' && (i - j < 0 || a[i - j] == '0') && j <= m) {b[i]= '1';}}}cout << b << endl;}return 0;//Time_test();
}