题目:https://codeforces.com/contest/1439/problem/A1
神奇大模拟
ac代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
typedef pair<int,int> PII;
const int N = 110;
char a[N][N];
int n , m ;
vector<PII> ans;
void ooo()
{
for(int i = 1; i <= n ; i ++)puts(a[i]+1);
cout <<"ans:" <<ans.size()<<endl;
cout <<endl;
}
void push3(int x ,int y)
{
//puts("bug3");
for(int i = x; i < x + 2 ; i ++)
for(int j = y ; j < y + 2; j ++)
{
if(a[i][j] == '1')ans.push_back({i,j});
a[i][j] = '0';
}
}
void push2(int x , int y)
{
PII temp[2] ;
int cnt = 0 ;
//puts("bug2");
for(int i = x ; i < x + 2; i ++)
for(int j = y ; j < y + 2; j ++)
{
if(a[i][j] == '1')temp[cnt++] = {i,j};
}
// for(int i = 0; i <= 1 ; i ++)
// {
// cout << temp[i].first<<' '<<temp[i].second <<endl;
// }
for(int i = x ; i < x + 2 ; i ++)
for(int j = y ; j < y + 2; j ++)
{
if(a[i][j] == '0')
{
ans.push_back({i,j});
a[i][j] = '1';
}
}
ans.push_back(temp[0]);
a[temp[0].first][temp[0].second] = '0';
push3(x,y);
}
void push1(int x , int y)
{
// puts("push1");
int potx , poty ;
for(int i = x ; i < x + 2; i ++)
for(int j = y ; j < y + 2 ; j ++)
{
if(a[i][j] == '1')
{
potx = i , poty = j ;
}
}
ans.push_back({potx,poty});
int num = 0 ;
for(int i = x ; i < x + 2; i ++)
for(int j = y ; j < y + 2; j ++)
{
if(a[i][j] == '0')
{
a[i][j] = '1';
ans.push_back({i,j});
num ++;
}
if(num == 2)
{
a[potx][poty] = '0';
push2(x,y);
return ;
}
}
}
void push4(int x, int y)
{
int num = 0 ;
for(int i = x ; i < x + 2; i ++)
for(int j = y ; j < y + 2; j ++)
{
if(a[i][j] == '1')
{
num ++ ;
a[i][j] = '0';
ans.push_back({i,j});
}
if(num == 3)
{
push1(x,y);
return ;
}
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while(t--)
{
ans.clear();
cin >> n >> m ;
for(int i = 1; i <= n ; i ++)
cin >> a[i] + 1;
for(int i = 1; i + 1 <= n ; i += 2)
{
for(int j = 1; j + 1 <= n ; j += 2)
{
// cout << i <<' '<< j << endl;
int num = 0 ;
for(int i1 = i ; i1 < i + 2; i1 ++)
for(int j1 = j ; j1 < j + 2 ;j1 ++)
{
if(a[i1][j1] == '1')num ++ ;
}
if(num == 0)continue;
else if(num == 1)push1(i,j);
else if(num == 2)push2(i,j);
else if(num == 3)push3(i,j);
else if(num == 4)push4(i,j);
// ooo();
}
}
// puts("status2");
for(int i = 1; i + 1 <= n ; i ++)
{
for(int j = m - 1; j >= 2; j -= 2)
{
int num = 0 ;
for(int i1 = i ; i1 < i + 2; i1 ++)
for(int j1 = j ; j1 < j + 2 ;j1 ++)
{
if(a[i1][j1] == '1')num ++ ;
}
if(num == 0)continue;
else if(num == 1)push1(i,j);
else if(num == 2)push2(i,j);
else if(num == 3)push3(i,j);
else if(num == 4)push4(i,j);
}
}
for(int i = n - 1; i >= 2 ; i -= 2)
{
for(int j = m - 1; j >= 2; j -= 2)
{
int num = 0 ;
for(int i1 = i ; i1 < i + 2; i1 ++)
for(int j1 = j ; j1 < j + 2 ;j1 ++)
{
if(a[i1][j1] == '1')num ++ ;
}
if(num == 0)continue;
else if(num == 1)push1(i,j);
else if(num == 2)push2(i,j);
else if(num == 3)push3(i,j);
else if(num == 4)push4(i,j);
// ooo();
}
}
for(int i = n - 1; i >= 2 ; i -= 2)
{
for(int j = 1; j + 1 <= m; j += 2)
{
int num = 0 ;
for(int i1 = i ; i1 < i + 2; i1 ++)
for(int j1 = j ; j1 < j + 2 ;j1 ++)
{
if(a[i1][j1] == '1')num ++ ;
}
if(num == 0)continue;
else if(num == 1)push1(i,j);
else if(num == 2)push2(i,j);
else if(num == 3)push3(i,j);
else if(num == 4)push4(i,j);
// ooo();
}
}
cout << ans.size()/3<<endl;
for(int i = 0 ; i < ans.size() ; i ++)
{
cout << ans[i].first<<' '<<ans[i].second<<' ';
if((i+1) % 3 == 0)cout <<endl;
}
}
}