I-The pilots ' refrigeratorTime
limit:MS
Memory Limit:65536KB
64bit IO Format:%i64d &%i64u SubmitStatus
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
/*author:2486memory:144 kbtime:360 mslanguage:c++result:accepted*/#include <cstdio> #include <cstring># Include <algorithm>using namespace Std;const int Maxn=1<<18;char maps[5][5];bool vis[maxn];int PATH[MAXN] ///record final path int pathvis[maxn];//record DFS process path int min;int binary (int val,int x,int y) {for (int i=0; i<16; i++) {//select row to fetch Anti-if (i/4==x| | i%4==y) {val^= (1<<i); }} return Val; void Dfs (int s,int step,int x,int y) {if (s==0) {if (min>step) {min=step; for (int i=0;i<step;i++) {path[i]=pathvis[i];//goes to the array that stores the final path if it is less than the previous step}} retur N } if (x>=4) return; int nx,ny; if (y+1>=4) {//GO right and then go down nx=x+1; ny=0; } else {ny=y+1; Nx=x; } int fd=x*4+y; int k=binary (s,x,y); pathvis[step]=fd;//the current position to reverse DFS (K,STEP+1,NX,NY); DFS (S,STEP,NX,NY);} int main () {while (~scanf ('%s ', Maps[0])) {for(int i=1; i<4; i++) {scanf ("%s", &maps[i]); } int s=0; for (int i=3, i>=0; i--) {for (int j=3; j>=0; j--) {if (maps[i][j]== ' + ') s=s<<1|1; else s<<=1; }} if (s==0) {printf ("0\n"); Continue } min=10000000; DFS (s,0,0,0); printf ("%d\n", Min); for (int i=0; i<min; i++) {printf ("%d%d\n", path[i]/4+1,path[i]%4+1); }} return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
The pilots ' Refrigerator-dfs path print