AcWing 95. 费解的开关
原题链接
中等
作者:
jiashenz
,
2020-02-18 04:01:35
,
所有人可见
,
阅读 727
#include <iostream>
using namespace std;
int N = 7, INF = 0x3f3f3f3f;
char a[N][N], b[N][N];
int dx[] = {-1, 1, 0, 0}, dy[] = {0, 0, -1, 1};
void changeLight(int x, int y)
{
a[x][y] = a[x][y] == '1' ? '0' : '1';
for (int i = 0; i < 4; i++) {
int nx = dx[i] + x;
int ny = dy[i] + y;
if (nx < 0 || nx == 5 || ny < 0 || ny == 5) continue;
a[nx][ny] = a[nx][ny] == '1' ? '0' : '1';
}
}
bool check()
{
for (int i = 0; i < 5; i++)
{
if (a[4][i] == '0') return false;
}
return true;
}
int main()
{
int n, ans;
cin >> n;
while (n--)
{
for (int i = 0; i < 5; i++) cin >> a[i];
ans = INF;
for (int i = 0; i < 1 << 5; i++)
{
memcpy(b, a, sizeof b);
int res = 0;
for (int j = 0; j < 5; j++)
{
if (i >> j & 1)
{
changeBit(0, j);
max++;
}
}
for (int k = 1; k < 5; k++)
{
for (int j = 0; j < 5; j++)
{
if (a[k-1][j] == '0')
{
changeBit(k, j);
max++;
}
}
}
if (check()) ans = min(ans, res);
memcpy(a, b, sizeof b);
}
if (ans > 6)
{
cout << "-1" << endl;
}
cout << ans << endl;
}
return 0;
}