首页 > 代码库 > poj 2965 The Pilots Brothers' refrigerator
poj 2965 The Pilots Brothers' refrigerator
The Pilots Brothers‘ refrigerator
Description The game “The Pilots Brothers: following the stripy elephant” has a quest where a player needs to open a refrigerator. There are 16 handles on the refrigerator door. Every handle can be in one of two states: open or closed. The refrigerator is open only when all handles are open. The handles are represented as a matrix 4х4. You can change the state of a handle in any location [i, j] (1 ≤ i, j ≤ 4). However, this also changes states of all handles in row i and all handles in column j. The task is to determine the minimum number of handle switching necessary to open the 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. Sample Input -+-- ---- ---- -+-- Sample Output 6 1 1 1 3 1 4 4 1 4 3 4 4 Source Northeastern Europe 2004, Western Subregion #include<iostream> #include<cstdio> #include<cstring> using namespace std; bool map[20][20]; int best=1000; int lj[70000][2]; int b[70000][2]; int l=0,t; int check()//查看门是否全开 { int i,j,tot=0; for(i=0;i<4;i++) for(j=0;j<4;j++) tot+=map[i][j]; if(tot==16) return 1; return 0; } void Change(int x,int y)//改变门的状态 { /* map[x][y]=!map[x][y]; map[x+1][y]=!map[x+1][y]; if(x>0)map[x-1][y]=!map[x-1][y]; map[x][y+1]=!map[x][y+1]; if(y>0)map[x][y-1]=!map[x][y-1];*/ int i,j; for(i=0;i<4;i++) map[x][i]=!map[x][i]; for(j=0;j<4;j++) map[j][y]=!map[j][y]; map[x][y]=!map[x][y];//该点被改回来,所以要在改变一次 } void Search(int k,int ans) { int x,y; int i; if(k==16) { if( check()&& ans<best ) { best=ans; for(i=0;i<best;i++) { b[i][1]=lj[i][1]; b[i][0]=lj[i][0]; } } } else {//cout<<ans<<endl; x=k/4; y=k%4; Search(k+1,ans); lj[l][0]=x+1; lj[l][1]=y+1; l++; Change(x,y); Search(k+1,ans+1); Change(x,y);//还原门的状态 l--; } } int main() { int i,j; char a; for(i=0;i<4;i++) { for(j=0;j<4;j++) { cin>>a; if(a=='-') map[i][j]=1; else map[i][j]=0; } } Search(0,0); if(best!=1000) cout<<best<<endl; else cout<<"Impossible"<<endl; for(i=0;i<best;i++) cout<<b[i][0]<<" "<<b[i][1]<<endl; return 0; } |