AcWing 95. 费解的开关
原题链接
中等
作者:
aiguazi
,
2025-04-24 12:59:25
· 江西
,
所有人可见
,
阅读 1
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int N = 6;
int n;
char g[N][N];
char bg[N][N];
int dx[5] = {-1, 0, 1, 0, 0}, dy[5] = {0, 1, 0, -1, 0};
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) continue;
g[a][b] ^= 1;
}
}
int main()
{
cin >> n;
while(n--)
{
int res = 10;
for (int i = 0; i < 5; i++) cin >> bg[i];
for (int op = 0; op < 32; op++)
{
int cnt = 0;
memcpy(g, bg, sizeof g);
for (int i = 0; i < 5; i++)
if (op >> i & 1)
{
turn(0, i);
cnt++;
}
for(int i = 0; i < 4; i++)
{
for (int j = 0; j < 5; j++)
{
if (g[i][j] == '0')
{
turn(i + 1, j);
cnt++;
}
}
}
bool success = true;
for (int i = 0; i < 5; i++)
{
if (g[4][i] == '0') success = false;
}
if (success && cnt < res) res = cnt;
}
if (res > 6) res = -1;
cout << res << endl;
}
return 0;
}