# POJ_2965_The Pilots Brothers' refrigerator_生生搜索

Input

The input contains four lines. Each of the four lines contains four characters describing the initial state of appropriate handles. A symbol “+” means that the handle is in closed state, whereas the symbol “−” means “open”. At least one of the handles is initially closed.

Output

The first line of the input contains N – the minimum number of switching. The rest N lines describe switching sequence. Each of the lines contains a row number and a column number of the matrix separated by one or more spaces. If there are several solutions, you may give any one of them.

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
using namespace std;
bool a[4][4];
struct item{
int mark[4][4];
bool operator == (const item& in)const{
for(int i=0;i<4;++i)
for(int j=0;j<4;++j)
if(mark[i][j]!=in.mark[i][j])
return false;
return true;
}
item& operator = (item& in){
for(int i=0;i<4;++i)
for(int j=0;j<4;++j)
mark[i][j]=in.mark[i][j];
return *this;
}
item(){memset(mark,0,sizeof(mark));}
};
item inf;
bool check(){
for(int i=0;i<4;++i)
for(int j=0;j<4;++j)
if(a[i][j])	return false;
return true;
}
void change(int x,int y){
for(int i=x;i>=0;--i)	a[i][y]=1-a[i][y];
for(int i=x+1;i<4;++i)	a[i][y]=1-a[i][y];
for(int i=y-1;i>=0;--i)	a[x][i]=1-a[x][i];
for(int i=y+1;i<4;++i)	a[x][i]=1-a[x][i];
}
int cnt(item in){
if(in.mark[0][0]==-1)	return 100;
int ret=0;
for(int i=0;i<4;++i)
for(int j=0;j<4;++j)
if(in.mark[i][j]==1)	++ret;
return ret;
}
item dfs(int x,int y,item flag){
item tem=flag;
if(x==4&&!y)
if(check())
return flag;
else	return inf;
int nxt_x=x, nxt_y=y+1;
if(y==3)	nxt_x=x+1,nxt_y=0;
item ret=dfs(nxt_x,nxt_y,flag);
change(x,y);
tem.mark[x][y]=1;
item temp=dfs(nxt_x,nxt_y,tem);
change(x,y);
if(cnt(ret)<cnt(temp))	return ret;
return temp;
}
int main(){
for(int i=0;i<4;++i)
for(int j=0;j<4;++j)	inf.mark[i][j]=-1;
memset(a,0,sizeof(a));
for(int i=0;i<4;++i)
for(int j=0;j<4;++j){
char tem;
cin>>tem;
if(tem=='+')	a[i][j]=1;
}
item in;
item ans=dfs(0,0,in);
if(ans==inf){	puts("ntmzdw");	return 0;}
int num=cnt(ans);
printf("%d\n",num);
for(int i=0;i<4;++i)
for(int j=0;j<4;++j)
if(ans.mark[i][j])
printf("%d %d\n",i+1,j+1);
return 0;
}

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客