Codeforces Round #684 (Div. 2) C1. Binary Table (Easy Version)
题意:有n*m个0,1组成的 数字矩阵,每次你可以选择一个大小为2 * 2的小方格,选择其中三个元素,对1进行异或运算(0变成1,1变成0)
思路:可以根据方格中的1的个数,分成5种情况,易得,第2种跟第3种情况第一步是一样的,可以归为一种。
#include <bits/stdc++.h>
using namespace std;
int a[200][200];
vector<vector<pair<int, int> > >v;
int work(int x, int y)
{
int cnt = 0;
for(int i = x; i <= x + 1; i++){
for(int j = y; j <= y + 1; j++){
if(a[i][j] == 1)
cnt++;
}
}
vector<pair<int, int> >ans;
if(cnt == 0){//全是0
return 1;
}
else if(cnt == 1 || cnt == 2){//1个1或者2个1
int c0 = 2, c1 = 1;
for(int i = x; i <= x + 1; i++){
for(int j = y; j <= y + 1; j++){
if(a[i][j] == 0 && c0){
ans.push_back(make_pair(i, j));
a[i][j] ^= 1;
c0--;
}else if(a[i][j] == 1 && c1){
ans.push_back(make_pair(i, j));
a[i][j] ^= 1;
c1--;
}
}
}
}
else if(cnt == 3){
for(int i = x; i <= x + 1; i++){
for(int j = y; j <= y + 1; j++){
if(a[i][j]){
ans.push_back(make_pair(i, j));
a[i][j] ^= 1;
}
}
}
}
else{
for(int i = x; i <= x + 1; i++){
for(int j = y; j <= y + 1; j++){
if(a[i][j] && ans.size() < 3){
ans.push_back(make_pair(i, j));
a[i][j] ^= 1;
}
}
}
}
v.push_back(ans);
return 0;
}
int main()
{
int t;
scanf("%d", &t);
while(t--){
v.clear();
int n, m;
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
scanf("%1d", &a[i][j]);
}
}
for(int i = 1; i < n; i++){
for(int j = 1; j < m; j++){
while(!work(i, j));
}
}
printf("%d\n", v.size());
for(int i = 0; i < v.size(); i++){
for(int j = 0; j < 3; j++){
pair<int, int>p = v[i][j];
printf("%d %d%c", p.first, p.second, " \n"[j == 2]);
}
}
}
return 0;
}