Description
The game "The pilots brothers:following the stripy elephant" has a quest where a player needs to open a refrigerator.
There is handles on the refrigerator door. Every handle can is in one of the States:open or closed. The refrigerator is open if all handles is open. The handles is 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 are 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 is several solutions, you could give any one of the them.
Sample Input
-+-----------+--
Sample Output
61 11 31 44 14 34 4
Test instructions: The elephant will open the refrigerator door and ask for the minimum number of steps required. The refrigerator has 16 switches, '-' means open, ' + ' means off, and when 16 switches are open, the refrigerator will open.
This problem is similar to POJ 1753, but it is important to note that flipping is the whole row.
1#include <iostream>2#include <stdio.h>3 using namespacestd;4 BOOLchess[6][6]= {{false}};5 intStep;6 BOOLFlag;7 intCot;8 intans[ -];9 BOOLReach_all ()//determine if it is a colorTen { One for(intI=1; i<5; i++) A { - for(intj=1; j<5; J + +) - { the if(Chess[i][j]) - return false; - } - } + return true; - } + voidTurnintRowintCol//Turn Chess A { at for(intI=1; i<5; i++) - { -chess[row][i]=!Chess[row][i]; -chess[i][col]=!Chess[i][col]; - } -chess[row][col]=!Chess[row][col]; in } - voidDfsintRowintColintDeep//deep search for iterative backtracking is important to { + if(deep==Step) - { theflag=Reach_all (); * return ; $ }Panax Notoginseng if(Flag| | row==5) - return ; theTurn (Row,col);//Turn Chess + if(col<4) ADFS (row,col+1, deep+1); the Else +DFS (row+1,1, deep+1); -Turn (Row,col);//if it doesn't fit, turn it back . $ if(flag) $ { -ans[cot++]=Row; -ans[cot++]=Col; the } - if(col<4)WuyiDFS (row,col+1, deep); the Else -DFS (row+1,1, deep); Wu return ; - } About intMain () $ { - Charch; - for(intI=1; i<5; i++) - { A for(intj=1; j<5; J + +) + { thescanf"%c",&ch); - if(ch=='+') $chess[i][j]=true; the } the GetChar (); the } the for(step=1; step<= -; step++)//Enumerate each of these cases - { incot=1; theDfs1,1,0); the if(flag) About Break; the } theprintf"%d\n", step); the for(inti=step*2; i>=1; i--) + if(i&1) -printf"%d%d\n", ans[i],ans[i+1]); the return 0;Bayi}
View Code
POJ 2965 the pilots ' refrigerator