Home Article Practice 费解开关

费解开关

2024-12-03 10:30  views:115  source:mmyyyy    

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int N = 6;
int dx[N] = {-1, 0, 1, 0, 0}, dy[N] = {0, 1, 0, -1, 0};
char g[N][N], backup[N][N];
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()
{
int n;
scanf("%d", &n);
while(n -- )
{
for (int i = 0; i < 5; i ++ ) cin >> g[i];
int res = 10;
for (int op = 0; op < 32; op ++ )
{
memcpy(backup, g, sizeof g);
int step = 0;
for (int i = 0; i < 5; i ++ )
if (op >> i & 1) {
step ++ ;
turn (0, i);;
}
for (int i =0; i < 4; i ++ )
for (int j = 0; j < 5;j ++ )
if (g[i][j] == '0')
{
step ++;
turn (i + 1, j);
}
bool dark = false;
for (int j = 0; j < 5; j ++ )
if (g[4][j] == '0')
{
dark = true;
break;
}
if (!dark) res = min(res, step);
memcpy (g, backup, sizeof g);
}
if(res > 6) res = -1;
cout << res << endl;
}
return 0;
}



Disclaimer: The above articles are added by users themselves and are only for typing and communication purposes. They do not represent the views of this website, and this website does not assume any legal responsibility. This statement is hereby made! If there is any infringement of your rights, please contact us promptly to delete it.

字符:    改为:
去打字就可以设置个性皮肤啦!(O ^ ~ ^ O)