51nod 1851俄罗斯方块(trick)

题目大意:给出一个黑白图,你可以选定一个俄罗斯方块的区域,黑白翻转,问能否变成白图

比较trick的题目,

首先可以想到,奇数个1肯定是无解的,所以考虑偶数个1

可以先讨论n是2的情况

当n为2时,其实除了m也等于2时需要特判外,都是可行的(因为你可以不断地往右侧推进,最后变成4个1)

所以n为偶数也是可行的

n为奇数时,可以把奇数行的1变到偶数行,所以也是可行的

最后就讨论n是1的情况,这个贪心往右侧走即可

(以及掌握了读入的优化技巧)

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int T, n, m;
int str[10000005];
int read01() {
    char c = getchar();
    for(; c != '0' && c != '1'; c = getchar());
    return c - '0';
}
int main() {
    cin>>T;
    while(T--) {
        scanf("%d %d", &n, &m);
        int ans = 0;
        if(n == 1 || m == 1) {
            n = (n > m ? n : m);
            for(int i = 0; i < n; i++)
                str[i] = read01();
            for(int i = 0; i < n-3; i++)
                if(str[i]) {
                    str[i] ^= 1;
                    str[i+1] ^= 1;
                    str[i+2] ^= 1;
                    str[i+3] ^= 1;
                }
            int f = 0;
            for(int i = 0; i < n; i++) if(str[i])  f = 1;
            if(f) cout<<"No"<<endl;
            else cout<<"Yes"<<endl;
            continue;
        }
        for(int i = 0; i < n; i++)
            for(int j = 0; j < m; j++)
                ans += read01();
        if(ans&1) cout<<"No"<<endl;
        else {
            if(n == 2 && m == 2) {
                if(ans == 4 || ans == 0) cout<<"Yes"<<endl;
                else cout<<"No"<<endl;
                continue;
            }
            cout<<"Yes"<<endl;
        }
    }
}
原文地址:https://www.cnblogs.com/Saurus/p/6901699.html