POJ1222 熄灯问题 穷举
一种poj1222 较短的实现,使用的穷举的算法,可能比高斯消元法容易理解一点
#include<iostream>
#include<bitset>
using namespace std;
void an(bool nob[5][6],int y,int x){
nob[y][x]^=1;
if(x>0)nob[y][x-1]^=1;
if(x<5)nob[y][x+1]^=1;
if(y>0)nob[y-1][x]^=1;
if(y<4)nob[y+1][x]^=1;
}
int main(){
bitset<6> bs;
bool inb[5][6],oub[5][6],nob[5][6];
int n;
cin>>n;
for(int sb=1;sb<=n;sb++){
for(int i=0;i<5;i++)
for(int j=0;j<6;j++)
cin>>inb[i][j];
for(int i=0;i<64;i++){
//置0与复制inb->nob
for(int j=0;j<5;j++)
for(int k=0;k<6;k++)
oub[j][k]=false,nob[j][k]=inb[j][k];
//第一层 按下的方法
bs=i;
for(int j=0;j<6;j++){
oub[0][j]=bs[5-j];
if(oub[0][j])
an(nob,0,j);
}
//由第一层的方法判断其余层
for(int j=1;j<5;j++)
for(int k=0;k<6;k++)
if(nob[j-1][k]){
oub[j][k]=true;
an(nob,j,k);
}
//判断并输出
if(!(nob[4][0]||nob[4][1]||nob[4][2]||nob[4][3]||nob[4][4]||nob[4][5])){
cout<<"PUZZLE #"<<sb<<endl;
for(int j=0;j<5;j++){
for(int k=0;k<5;k++)
cout<<oub[j][k]<<" ";
cout<<oub[j][5];
cout<<endl;
}
}
}
}
}
浙公网安备 33010602011771号