Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 15399 | Accepted: 5776 | Special Judge |
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 rowi and all handles in columnj.
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
在1753上略作修改
#include <iostream> #include <cstring> using namespace std; int a[6][6]; int r[17],c[17]; bool flag = false; int step; bool check(){ for(int i=1;i<5;i++) for(int j=1;j<5;j++) if (a[i][j]!=1) return false; return true; } void flip(int row,int col){ a[1][col] = (a[1][col]+1)%2; a[2][col] = (a[2][col]+1)%2; a[3][col] = (a[3][col]+1)%2; a[4][col] = (a[4][col]+1)%2; a[row][1] = (a[row][1]+1)%2; a[row][2] = (a[row][2]+1)%2; a[row][3] = (a[row][3]+1)%2; a[row][4] = (a[row][4]+1)%2; a[row][col] = (a[row][col]+1)%2; return; } void sear(int row,int col,int deep){ if(deep==step) { flag=check(); return; } if(flag||row==5)return; flip(row,col); r[deep] = row; c[deep] = col; if(col<4) sear(row,col+1,deep+1); else sear(row+1,1,deep+1); flip(row,col); if(col<4) sear(row,col+1,deep); else sear(row+1,1,deep); return; } int main() { char b; memset(a,0,sizeof(a)); for(int i=1;i<5;i++) for(int j=1;j<5;j++) { cin>>b; if(b=='-') a[i][j] = 1; } for(step=0;step<=16;step++){ sear(1,1,0); if(flag) break; } if(flag){ cout<<step<<endl; for(int j=0;j<=17;j++) if(c[j]!=0&&r[j]!=0) cout<<r[j]<<" "<<c[j]<<endl; } else cout<<"Impossible"<<endl; return 0; }