你玩过“拉灯”游戏吗?25盏灯排成一个5x5的方形。每一个灯都有一个开关,游戏者可以改变它的状态。每一步,游戏者可以改变某一个灯的状态。游戏者改变一个灯的状态会产生连锁反应:和这个灯上下左右相邻的灯也要相应地改变其状态。

我们用数字“1”表示一盏开着的灯,用数字“0”表示关着的灯。下面这种状态

10111
01101
10111
10000
11011

在改变了最左上角的灯的状态后将变成:

01111
11101
10111
10000
11011

再改变它正中间的灯后状态将变成:

01111
11001
11001
10100
11011

给定一些游戏的初始状态,编写程序判断游戏者是否可能在6步以内使所有的灯都变亮。
输入格式

第一行输入正整数n
,代表数据***有n

个待解决的游戏初始状态。

以下若干行数据分为n

组,每组数据有5行,每行5个字符。每组数据描述了一个游戏的初始状态。各组数据间用一个空行分隔。
输出格式

一共输出n

行数据,每行有一个小于等于6的整数,它表示对于输入数据中对应的游戏状态最少需要几步才能使所有灯变亮。

对于某一个游戏初始状态,若6步以内无法使所有灯变亮,则输出“-1”。
数据范围

0<n≤500

输入样例:

3
00111
01011
10001
11010
11100

11101
11101
11110
11111
11111

01111
11111
11111
11111
11111

输出样例:

3
2
-1
题目链接 : https://www.acwing.com/problem/content/description/97/

经典题目:

  • 枚举第一行每个位置按或者不按的所有状态
  • 递推出 2 , 3 , 4 , 5 2,3,4,5 2,3,4,5行,如果最后一行不是全亮,则不是一个解决方案
  • 复杂度 O ( 2 5 5 20 500 ) O(2^5*5*20*500) O(25520500)
  • 题目要求输出步数小于6的最小方案
  • y总的讲题视频:https://www.acwing.com/video/114/

代码如下:

#ifdef debug
#include <time.h>
#include "/home/majiao/mb.h"
#endif

#include <iostream>
#include <algorithm>
#include <vector>
#include <string.h>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <math.h>

#define MAXN ((int)1e5+7)
#define ll long long int
#define INF (0x7f7f7f7f)
#define fori(lef, rig) for(int i=lef; i<=rig; i++)
#define forj(lef, rig) for(int j=lef; j<=rig; j++)
#define fork(lef, rig) for(int k=lef; k<=rig; k++)
#define QAQ (0)

using namespace std;

#define show(x...) \ do { \ cout << "\033[31;1m " << #x << " -> "; \ err(x); \ } while (0)

void err() { cout << "\033[39;0m" << endl; }
template<typename T, typename... A>
void err(T a, A... x) { cout << a << ' '; err(x...); }

namespace FastIO{

	char print_f[105];
	void read() {}
	void print() { putchar('\n'); }

	template <typename T, typename... T2>
		inline void read(T &x, T2 &... oth) {
			x = 0;
			char ch = getchar();
			ll f = 1;
			while (!isdigit(ch)) {
				if (ch == '-') f *= -1; 
				ch = getchar();
			}
			while (isdigit(ch)) {
				x = x * 10 + ch - 48;
				ch = getchar();
			}
			x *= f;
			read(oth...);
		}
	template <typename T, typename... T2>
		inline void print(T x, T2... oth) {
			ll p3=-1;
			if(x<0) putchar('-'), x=-x;
			do{
				print_f[++p3] = x%10 + 48;
			} while(x/=10);
			while(p3>=0) putchar(print_f[p3--]);
			putchar(' ');
			print(oth...);
		}
} // namespace FastIO
using FastIO::print;
using FastIO::read;

int n, m, Q, K;
int dr[] = { 0, 1, -1, 0, 0 };
int dc[] = { 0, 0, 0, 1, -1 };
char mtx[8][8];

void turn(char p[][8], int r, int c) { //模拟按下一个位置后改变周围
	for(int i=0; i<5; i++) {
		int nr = r+dr[i], nc = c+dc[i];
		if(nr>=1 && nr<=5 && nc>=1 && nc<=5)
			p[nr][nc] ^= 1; //'0'==48, '1'==49, (48 ^= 1) == 49
	}
}

int slove() {
	int ans = 0x3f3f3f3f;

	for(int i=0; i<(1<<5); i++) { //枚举第一行的状态2^5种方案
		int x = i, cnt = 0;
		char tmp[8][8];
		memcpy(tmp, mtx, sizeof(mtx));

		for(int j=1; x; j++, x>>=1) //按第一行
			if(x & 1) turn(tmp, 1, j), cnt ++;

		/** * 如果上一行这个位置mtx[j-1][k]是熄灭状态, * 则这个位置mtx[j][k]一定要按下, 把上一个修改为亮 */
		for(int j=1; j<=4; j++) //递推出2到4行按或不按
			for(int k=1; k<=5; k++)
				if(tmp[j][k] == '0') turn(tmp, j+1, k), cnt ++;
		bool ok = true;
		for(int j=1; j<=5; j++) //判断最后一行是否全亮即可
			if(tmp[5][j] == '0') ok = false;
		if(ok) ans = min(ans, cnt);
	}
	return ans <= 6 ? ans : -1; //题目要求6步以内完成
}

int main() {
#ifdef debug
	freopen("test", "r", stdin);
	clock_t stime = clock();
#endif
	scanf("%d ", &Q);
	while(Q--) {
		for(int i=1; i<=5; i++)
			scanf("%s ", mtx[i]+1);
		printf("%d\n", slove());
	}





#ifdef debug
	clock_t etime = clock();
	printf("rum time: %lf 秒\n",(double) (etime-stime)/CLOCKS_PER_SEC);
#endif 
	return 0;
}