cf1523A. Game of Life
生活随笔
收集整理的這篇文章主要介紹了
cf1523A. Game of Life
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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(); }總結
以上是生活随笔為你收集整理的cf1523A. Game of Life的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 笑的时候嘴抖动怎么回事?
- 下一篇: 牙与大脑的神经有关吗?