Reprint please indicate the source: http://blog.csdn.net/u012860063? Viewmode = Contents
Question link: http://poj.org/problem? Id = 2965
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
Reprint ():
Proof: to change a '+' symbol to '-', the corresponding row and column operations must be odd;
It can be proved that if an operation is performed at each position of the row and column corresponding to the '+' position, only the symbol of the '+' position in the entire graph changes, and the rest will not change.
> Set a 4*4 integer array with the initial value of zero to record the operands of each vertex. Then, 1 is added to the positions of rows and columns on each '+, expected result 2 is displayed.
(Because the effect of performing an even number of operations on a vertex is the same as that of not performing any operation, this is the principle of the inverse operation mentioned above ),
Then calculate
> The number is the operand, and the position of the first operation is the location where the operation is to be performed (other operations with the original number being an even number do not work, so no operation is performed)
Code 1: (directly calculate the result)
# Include <iostream> # include <cstring> using namespace STD; int map [5] [5]; int ans; char a [8]; void count (int I, int J) {int K; For (k = 0; k <4; k ++) {map [I] [k] ++; map [k] [J] ++;} map [I] [J] --; // because the preceding for operation is performed twice in map [I] [J] ++, this operation is subtracted once} void result () {ans = 0; int I, J; for (I = 0; I <4; I ++) for (j = 0; j <4; j ++) ans + = map [I] [J] % 2;} void output () {cout <ans <Endl; int I, j; for (I = 0; I <4; I ++) for (j = 0; j <4; j ++) if (Map [I] [J] % 2 = 1) cout <I + 1 <''<j + 1 <Endl;} int main () {memset (MAP, 0, sizeof (MAP); int I; int J; for (I = 0; I <4; I ++) {CIN> A; For (j = 0; j <4; j ++) if (A [J] = '+') count (I, j) ;}result (); output (); Return 0 ;}
Code 2 (Enum + DFS) is as follows:
# Include <cstring> # include <iostream> using namespace STD; // This question is not suitable for BFS because it is necessary to output the piece to be flipped each time, use the DFS output full path bool lock [10] [10]; // actually only use row3: → row6, col3: → col6; bool flag; int step; int Ri [17], CJ [17]; bool isopen () {for (INT I = 3; I <7; I ++) {for (Int J = 3; j <7; j ++) {If (Lock [I] [J]! = True) return false ;}} return true;} void flip (INT row, int col) // Changes the status {lock [row] [col] =! Lock [row] [col]; for (INT I = 3; I <7; I ++) {lock [I] [col] =! Lock [I] [col] ;}for (Int J = 3; j <7; j ++) {lock [row] [J] =! Lock [row] [J];} return;} void DFS (INT row, int Col, int deep) {If (step = Deep) {flag = isopen (); return;} If (flag | ROW = 7) return; flip (row, col); ri [Deep] = row; // record path CJ [Deep] = Col; if (COL <6) DFS (row, Col + 1, deep + 1); elsedfs (row + 1, 3, deep + 1); // flip (row, COL); // If the status does not match, change it back if (COL <6) DFS (row, Col + 1, deep); elsedfs (row + 1, 3, deep ); return;} int main () {char temp; int I, j; memset (lock, false, sizeof (LOCK); for (I = 3; I <7; I ++) {for (j = 3; j <7; j ++) {CIN> temp; If (temp = '-') {lock [I] [J] = true ;}}for (step = 0; Step <= 16; Step ++) // enumerative steps {DFS (3, 3, 0); If (FLAG) break;} cout <step <Endl; for (I = 0; I <step; I ++) {cout <Ri [I]-2 <''<cj [I]-2 <Endl;} return 0 ;}