http://poj.org/problem?id=2965
POJ 1753 extension, dfs+ enumeration, but adds a path.
The pilots ' refrigerator
Time Limit: 1000MS |
|
Memory Limit: 65536K |
Total Submissions: 19278 |
|
Accepted: 7384 |
|
Special Judge |
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
#include <iostream>#include<cstdio>#include<cstring>using namespacestd;intans[6][6];intStep;intr[ -],c[ -];BOOLflag=false;BOOLjudge () {inti,j,sum=0; for(i=1; i<=4; i++) for(j=1; j<=4; j + +) Sum+=Ans[i][j]; if(sum==0) return true; Else return false;}voidFlipintXinty) { inti,j; for(j=1; j<=4; j + +) { if(ans[x][j]==1) Ans[x][j]=0; ElseAns[x][j]=1; } for(i=1; i<=4; i++) { if(i==x)Continue; if(ans[i][y]==1) Ans[i][y]=0; ElseAns[i][y]=1; }}voidDfsintXintYintd) {if(d==Step) {Flag=judge (); return ; } if(Flag| | X>4) { return ; } flip (x, y); R[D]=x;//Save the path. C[d]=y;//Save the path. if(y<4) Dfs (x, y+1, d+1); ElseDFS (x+1,1, d+1); Flip (x, y); if(y<4) Dfs (x, y+1, D); ElseDFS (x+1,1, D); }intMain () {inti,j; Chartemp; for(i=1; i<=4; i++) { for(j=1; j<=4; j + +) {scanf ("%c",&temp); if(temp=='+') Ans[i][j]=1; ElseAns[i][j]=0; } getchar (); } for(step=1; step<= -; step++) {DFS (1,1,0); if(flag) Break; } printf ("%d\n", step); for(i=0; i<step;i++) printf ("%d%d\n", R[i],c[i]); return 0;}
POJ 2965 the pilots ' refrigerator