95. 费解的开关
作者:
__NULL__
,
2024-09-01 20:30:10
,
所有人可见
,
阅读 68
#include<bits/stdc++.h>
using namespace std;
const int dx[] = {0, 1, -1, 0, 0, 0}, dy[] = {0, 0, 0, 1, -1, 0}, MAXX = 10;
int n, t, a[MAXX][MAXX], ans, summ;
char c;
void dfs(int sum){
if(sum > 6 || sum >= ans){
//cout << " hh " << " " << sum << " " << ans << " " << '\n';
return;
}
int x = 0, y = 0;
for(int i = 1; i <= 5 && x == 0; i++){
for(int j = 1; j <= 5 && x == 0; j++){
//cout << i << " " << j << " "<< a[i][j] << '\n';
if(a[i][j] == 0){
x = i, y = j;
break;
}
}
}
if(x == 0){
//cout << 114514 << '\n';
ans = min(ans, sum);
return;
}
//cout << x << " " << y << '\n';
for(int i = 0; i <= 4; i++){
int xx = x + dx[i], yy = y + dy[i];
if(xx >= 1 && xx <= 5 && yy >= 1 && yy <= 5){
summ++;
//cout << sum << " " << xx << " " << yy << " " << i << '\n';
for(int j = 0; j <= 4; j++){
int xxx = xx + dx[j], yyy = yy + dy[j];
a[xxx][yyy] = abs(a[xxx][yyy] - 1);
//cout << " " << xxx << " " << yyy << " " << a[xxx][yyy] << '\n';
}
dfs(sum + 1);
for(int j = 0; j <= 4; j++){
int xxx = xx + dx[j], yyy = yy + dy[j];
a[xxx][yyy] = abs(a[xxx][yyy] - 1);
}
}
}
}
int main(){
cin >> t;
while(t--){
for(int i = 1; i <= 5; i++){
for(int j = 1; j <= 5; j++){
cin >> c;
if(c == '0'){
a[i][j] = 0;
}else{
a[i][j] = 1;
}
}
}
ans = 114514;
dfs(0);
if(ans <= 6){
cout << ans << '\n';
}else{
cout << -1 << '\n';
}
//cout << summ << '\n';
}
return 0;
}
/*
1
00111
01111
11111
11111
11111
*/