1,cf Putting Bricks in the Wall
1,
对于这种数据的输入,一定要长点心!!如果要双层循环输入的话,必须是逐个空格隔开的数据!对于该数据而言,只能逐行输入!!;
题意:小明要从(1,1)走到(n,n),一开始他可以选择0或者1,之后在上下左右四个方向走与他所选的数字相同的格子;你是坏蛋,要阻止他走到终点,你最多有两次操作:将单元格的数字翻转,1变0,0变1;总是存在解决方案的,输出操作次数和翻转格子的坐标;
思路:起点终点“互质”即可:包围他俩的格子组成:1100或者0011的序列即可;
所以只需要进行判断即可;
细节:上面说到的输入;还有就是一定要把函数意识建立起来!!一开始输出操作次数,我直接
cout<<check1100()<<endl;这是错误的!!,这会导致该函数在执行一次!,然后存储的下标会在存储一遍;所以有时候你想要的是函数的结果还是过程,一定要区分好再利用!!
#pragma GCC optimize(2)
#include<bits/stdc++.h>
#define rep1(i,a,n) for( int i=a;i<n;++i)
#define rep2(i,a,n) for( int i=a;i<=n;++i)
#define per1(i,n,a) for( int i=n;i>a;i--)
#define per2(i,n,a) for( int i=n;i>=a;i--)
#define quick_cin() cin.tie(0),cout.tie(0),ios::sync_with_stdio(false)
#define memset(a,i) memset((a),(i),sizeof (a))
#define memcpy(a,i) memcpy((a),(i),sizeof (a))
#define pro_q priority_queue
#define pb push_back
#define pf push_front
#define endl "\n"
#define lowbit(m) (-m&m)
#define YES cout<<"YES\n"
#define NO cout<<"NO\n"
#define Yes cout<<"Yes\n"
#define No cout<<"No\n"
#define yes cout<<"yes\n"
#define no cout<<"no\n"
#define yi first
#define er second
using namespace std;
typedef pair<int,int> PII;
typedef pair<double,int>PDI;
typedef pair<long long,long long>PLL;
typedef pair<int,PII> PIII;
typedef long long ll;
typedef double dob;
const int N=1e3+10;
char a[N][N];
char s[5];
char s1100[]={'1','1','0','0'};
char s0011[]={'0','0','1','1'};
int n;
vector<PII>zb1100,zb0011;
int check1100()
{
if(!strcmp(s,s1100))return 0;
int num=0;
if(s[0]!='1')
num++,zb1100.pb({1,2});
if(s[1]!='1')
num++,zb1100.pb({2,1});
if(s[2]!='0')
num++,zb1100.pb({n-1,n});
if(s[3]!='0')
num++,zb1100.pb({n,n-1});
return num;
}
int check0011()
{
if(!strcmp(s,s0011))return 0;
int num=0;
if(s[0]!='0')
num++,zb0011.pb({1,2});
if(s[1]!='0')
num++,zb0011.pb({2,1});
if(s[2]!='1')
num++,zb0011.pb({n-1,n});
if(s[3]!='1')
num++,zb0011.pb({n,n-1});
return num;
}
void solve()
{
cin>>n;
rep1(i,0,n)cin>>a[i];
zb0011.clear();
zb1100.clear();
s[0]=a[0][1];
s[1]=a[1][0];
s[2]=a[n-2][n-1];
s[3]=a[n-1][n-2];
if(!check1100()||!check0011())cout<<0<<endl;
else
{
if(zb1100.size()<3)
{
cout<<zb1100.size()<<endl;
for(auto i:zb1100)cout<<i.first<<" "<<i.second<<endl;
}
else
{
cout<<zb0011.size()<<endl;
for(auto i:zb0011)cout<<i.first<<" "<<i.second<<endl;
}
}
}
signed main()
{
quick_cin();
int T;
cin>>T;
while(T--)solve();
return 0;
}