#include<iostream>
#include<string>
using namespace std;
int a[5][5];
int vis[5][5];
int stepmmm = 114514;
int kkk;
bool judge() {
int bb = false;
for(int i=0; i<5; i++) {
for(int j=0; j<5; j++) {
if(a[i][j] == 0) {
bb = true;
}
// cout<<a[i][j];
}
// cout<<endl;
}
// cout<<endl;
return !bb; // true为全开 false为不可全开
}
void batterfly(int x, int y) {
int x1 = x - 1;
int x2 = x + 1;
int y1 = y - 1;
int y2 = y + 1;
if(x1>-1) {
a[x1][y] = 1 - a[x1][y];
}
if(x2<5) {
a[x2][y] = 1 - a[x2][y];
}
if(y1>-1) {
a[x][y1] = 1 - a[x][y1];
}
if(y2<5) {
a[x][y2] = 1 - a[x][y2];
}
}
void print(int num) { // 查看次数
cout<<endl;
for(int i=0;i<5;i++){
for(int j=0;j<5;j++){
cout<<a[i][j];
}
cout<<endl;
}
kkk++;
if(kkk>num){
exit(0);
}
}
void f(int step) {
if(judge() && (step-1)<=6) {
stepmmm = min(stepmmm, step-1);
for(int i=0;i<5;i++){
for(int j=0;j<5;j++){
cout<<a[i][j];
}
cout<<endl;
}
cout<<endl;
return;
}
if((step-1) > 6) {
return;
}
for(int i=0; i<5; i++) {
for(int j=0; j<5; j++) {
if(a[i][j] == 0) {
a[i][j] = 1;
// 连锁
batterfly(i, j);
f(step+1);
a[i][j] = 0;
batterfly(i, j);
}
}
}
}
int main() {
int n;
cin>>n;
char s[5]; // 接受长数据
while(n!=0) {
stepmmm = 114514;
for(int i=0; i<5; i++) {
cin>>s;
for(int q =0; q<5; q++) {
a[i][q] = s[q] - '0';
}
// 0关 1开
}
f(1);
if(stepmmm==114514) {
cout<<stepmmm<<endl;
} else {
cout<<stepmmm<<endl;
}
n--;
}
return 0;
}
这是拉灯的一种类型题目,题目为:你玩过“拉灯”游戏吗?25 盏灯排成一个 5x5 的方形。每一个灯都有一个开关,游戏者可以改变它的状态。每一步,游戏者可以改变某一个灯的状态。游戏者改变一个灯的状态会产生连锁反应:和这个灯上下左右相邻的灯也要相应地改变其状态。
我用bfs来写,可是为什么如下样例,我带上去显示输出为4
1
11101
11101
11110
11111
11111
正确答案是2
感谢每一个为我解答的人.