#include <iostream>
#include <cstring>
using namespace std;
const int N = 510;
char g[N][N];
int dx[] = {0, 1, 0, -1, 0}, dy[] = {0, 0, 1, 0, -1};
void turn(int x, int y)
{
for (int i = 0; i < 5; i ++ )
{
int a = x + dx[i], b = y + dy[i];
if (a >= 0 && a < 5 && b >= 0 && b < 5)
g[a][b] ^= 1;
}
}
int main()
{
int t;
cin >> t;
while (t -- )
{
int ans = 1e9;
for (int i = 0; i < 5; i ++ ) cin >> g[i];
for (int i = 0; i < 1 << 5; i ++ )
{
char backup[N][N];
memcpy(backup, g, sizeof g);
int res = 0;
for (int j = 0; j < 5; j ++ )
if (i >> j & 1)
{
res ++ ;
turn(0, j);
}
for (int u = 0; u < 4; u ++ )
for (int v = 0; v < 5; v ++ )
if (g[u][v] == '0')
{
res ++ ;
turn(u + 1, v);
}
bool successful = true;
for (int i = 0; i < 5; i ++ )
if (g[4][i] == '0')
successful = false;
if (successful) ans = min(ans, res);
//memcpy(g, backup, sizeof g);
}
if (ans <= 6) cout << ans << endl;
else puts("-1");
}
return 0;
}
费解的开关——Acwing95
最新推荐文章于 2024-11-05 21:58:11 发布