Problem Description:
There are m x N (m<=100, n<=100) gold coins lined up on the desktop into an M-row N-column coin array. Each coin is either facing up or facing up on the back. The gold status is indicated by a number, 0 means the gold coin is facing up, and 1 means the back is facing up. The rules of the coin array game are:
(1) Each line of gold coins can be turned over and placed in the original position;
(2) You can choose 2 columns at a time to exchange the positions of these 2 gold coins.
Algorithm design:
Given the initial and target states of a coin array, the minimum number of transformations required to transform the gold array from the initial state to the target State is calculated by the game of gold rule.
Data input:
The input data is given by the file input.txt. There are multiple sets of data in the file. The 1th line of the file has 1 positive integer k, which indicates that there is a K-group of data. The 1th row of each group of data has 2 positive integers m and N. The following m line is the initial state of the gold array, each row has n digits indicating the status of the gold coin, 0 means the gold coin is facing up, and 1 is the back face up. The next M-line is the target state of the gold array.
Result output:
The minimum number of transformations calculated is output to the file Output.txt in the order of the input data. Output-1 when the corresponding data is not solved.
Example output file for input file sample
Input.txt
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
1 0 0
1 1 1
1 1 0
1 1 1
0 1 1
1 0 1
Output.txt
2
-1
Leave the pit ...
Coin array Problem