Http://poj.org/problem? Id = 2965
The pilots brothers 'refrigerator
Time limit:1000 ms |
|
Memory limit:65536 K |
Total submissions:10158 |
|
Accepted:3707 |
|
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 records 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 rowIAnd 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
61 11 31 44 14 34 4
Source
Northeastern Europe 2004, western subregion
Question Analysis: The refrigerator has 16 handles, each of which is in two States (Open '-' or close '+'). The door is opened only when all the handles are opened, the input data is a 4*4 matrix, So bit representation is considered. You can change the position of any handle, but change the row and column of the handle at the same time. Find the minimum step, so Algorithm Is a bitmap + wide search. If the current status already exists in the queue, you do not need to join the queue again, so you need to determine the weight. A Status of 2 ^ 16-1, ending and exiting at 0. A variable is required to record the current state, and a variable is transferred from the position in the queue. There are 16 status change schemes after one status. Bit operation, BFs, and then pay attention to the record path!
# Include < Stdio. h >
# Include < Iostream >
# Include < String . H >
Using Namespace STD;
Const Int Maxn = 65536 ;
Int Que [maxn * 2 ];
Int Pre [maxn];
Int Prei [maxn];
Int Prej [maxn];
Int Step [maxn];
Void BFS ( Int P)
{
Int I, j, rear, front;
Rear = Front = 0 ;
Que [rear ++ ] = P;
Memset (PRE, - 1 , Sizeof (Pre ));
Memset (step, 0 , Sizeof (STEP ));
Memset (prei, - 1 , Sizeof (Prei ));
Memset (prej, - 1 , Sizeof (Prej ));
Pre [p] = P;
// Step [p] ++;
While (Front < Rear)
{
Int TMP = Que [Front ++ ];
Int T = TMP;
For (I = 0 ; I <= 3 ; I ++ )
For (J = 0 ; J <= 3 ; J ++ )
{
TMP = T;
TMP ^ = 1 < ( 15 - J );
TMP ^ = 1 < ( 15 - J - 4 );
TMP ^ = 1 < ( 15 - J - 8 );
TMP ^ = 1 < ( 15 - J - 12 );
TMP ^ = 1 < ( 15 - 4 * I );
TMP ^ = 1 < ( 15 - 4 * I - 1 );
TMP ^ = 1 < ( 15 - 4 * I - 2 );
TMP ^ = 1 < ( 15 - 4 * I - 3 );
TMP ^ = 1 < ( 15 - 4 * I - J ); // (I, j) one more time.
If (Pre [TMP] =- 1 )
{
Pre [TMP] = T;
Step [TMP] = Step [T] + 1 ;
Prei [TMP] = I + 1 ;
Prej [TMP] = J + 1 ;
Que [rear ++ ] = TMP;
}
If (TMP = 0 )
{
Printf ( " % D \ n " , Step [TMP]);
Int X, Y, N;
Int A [maxn], B [maxn];
N = X = Y = Step [TMP];
While (Pre [TMP] ! = TMP)
{
A [x -- ] = Prei [TMP];
B [y -- ] = Prej [TMP];
TMP = Pre [TMP];
}
For ( Int K = 1 ; K <= N; k ++ )
{
Printf ( " % D \ n " , A [K], B [k]);
}
Return ;
}
}
}
}
Int Main ()
{
Freopen ( " Test. In " , " R " , Stdin );
Freopen ( " Test. Out " , " W " , Stdout );
Int N = 0 ;
Char Ch;
Int I, J;
For (I = 0 ; I < 4 ; I ++ )
For (J = 0 ; J < 4 ; J ++ )
{
CIN > Ch; n <= 1 ;
If (CH = ' + ' ) N + = 1 ;
}
BFS (N );
Return 0 ;
}