题目大意:给出一个黑白图,你能够选定一个俄罗斯方块的区域,黑白翻转,问可否变成白图ios
比较trick的题目,优化
首先能够想到,奇数个1确定是无解的,因此考虑偶数个1spa
能够先讨论n是2的状况code
当n为2时,其实除了m也等于2时须要特判外,都是可行的(由于你能够不断地往右侧推动,最后变成4个1)blog
因此n为偶数也是可行的ci
n为奇数时,能够把奇数行的1变到偶数行,因此也是可行的get
最后就讨论n是1的状况,这个贪心往右侧走便可string
(以及掌握了读入的优化技巧)io
#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; } } }