Description:
On the desktop there are coins m * n (m <= 100, n <= 100), which form the matrix of the coin m row n. Each coin is either facing up or facing back, indicated by 0 or 1.
Rules of the game:
(1) Each time you are allowed to invert one row of coins.
(2) every time, you can swap two columns.
An object:
from source matrix → target matrix
Input:
1. k - test test counter 2. mn number of rows and columns
3. inital matrix and target matrix numbers
The output is the smallest steps from the source matrix to the target matrix, if it is impossible to transfer from the initial to the target, the output is -1.
sample intput
2
4 3
1 0 1
0 0 0
1 1 0
1 0 1
1 0 1
1 1 1
0 1 1
1 0 1
4 3
1 0 1
0 0 0
one hundred
1 1 1
1 1 0
1 1 1
0 1 1
1 0 1
sample output
2
1
I encoded one solution: mysolution.cc , which lists all the features and what's right, but it's too slow, you could provide the right, but quick solution.
Thanks.
source share