C language: problems with the gold coin Array

Source: Internet
Author: User

There are m * n (m <= 100, n <= 100) gold coins on the desktop in a m row n columns of gold coins array. Each gold coin or front or back facing up. The number indicates the gold coin status. 0 indicates that the gold coin is facing up, and 1 indicates that the back is facing up.

The rules of the gold coin array game are as follows: (1) Each time any row of gold coins can be turned over and placed on the original location;

(2) You can select two columns each time to exchange the positions of these two columns of gold coins.

Programming tasks: given the initial status and target status of the gold coin array, the minimum number of changes required to change the gold coin array from the initial status to the target status according to the rules of the gold coin game.

Input

Multiple groups of data are input. Row 1st has a positive integer k, indicating that there are k groups of data. The first row of each data group has two positive integers m and n. The following m rows are the initial status of the gold coin array. Each row has n numbers to indicate the status of the gold coin for this row. 0 indicates that the gold coin is facing up, and 1 indicates that the back is facing up. The next m row is the target status of the gold coin array.

Output

Output the calculated minimum number of transformations in the order of input data. Output-1 when no data is available.

The code is written by others. I wrote this blog to review my ideas.

Enumeration 1 ~ In m, when each column is the first column,

// From 1 ~ N rows to find the rows that do not meet the requirements and perform a row transformation

// If the enumerated column can be successfully converted to the target matrix according to the rule, the difference between the matrix and the original matrix will only be in the column order.

In this case, the column I = 2 (the second column) is compared with the column I of the target matrix,

If different, find column j in this matrix (= I + 1 ~ M) Is there the same column I as the target matrix? If yes, and column j in this matrix! = Column j of the target matrix, a column transformation is performed.

// If no matching column is found, the first column of the enumerated Column cannot be converted to the target matrix according to the given rule.

Copy codeThe Code is as follows: # include <stdio. h>

Constint inf = 99999;
Const int n= 101;

Int a [N] [N], B [N] [N], temp [N] [N]; // a stores the initial matrix, and B is the target State Matrix.
Int n, m;
Int need; // number of times to be changed

Void ChangeL (int x, int y) // convert a column
{
If (x = y) return;
Int I;
For (I = 1; I <= n; I ++)
{
Int tt = temp [I] [y];
Temp [I] [y] = temp [I] [x];
Temp [I] [x] = tt;
}
Need ++;
}

Void ChangeH (int x) // convert rows
{
Int I;
For (I = 1; I <= m; I ++)
{
Temp [x] [I] ^ = 1;
}
}

Bool Same (int x, int y) // determines whether the column meets the conditions
{
Int I;
For (I = 1; I <= n; I ++)
If (B [I] [x]! = Temp [I] [y]) return false;
Return true;
}

Int main ()
{
Int tests;
Scanf ("% d", & tests); // number of data groups

While (tests --)
{
Scanf ("% d", & n, & m); // n rows, m Columns
Int I, j;
For (I = 1; I <= n; I ++)
For (j = 1; j <= m; j ++)
{
Scanf ("% d", & a [I] [j]);
}

For (I = 1; I <= n; I ++)
For (j = 1; j <= m; j ++)
Scanf ("% d", & B [I] [j]);

Int k;
Int ans = inf; // the final answer stored by ans. The initial value is infinity.

For (k = 1; k <= m; k ++) // enumerate each column as the first column
{
For (I = 1; I <= n; I ++)
For (j = 1; j <= m; j ++)
Temp [I] [j] = a [I] [j];
Need = 0;
ChangeL (1, k );

// Perform a transformation for the rows that do not meet the requirements
For (I = 1; I <= n; I ++)
{
If (temp [I] [1]! = B [I] [1]) // This row does not meet the conditions
{
ChangeH (I); // converts rows.
Need ++;
}
}

Bool find;
For (I = 1; I <= m; I ++) // check whether each column meets the conditions
{
Find = false;
If (Same (I, I ))
{
Find = true;
Continue;
}
For (j = I + 1; j <= m; j ++) // search for columns with the same I as B in temp
{
If (Same (I, j) // The j column of temp is the Same as the I column of B.
{
If (Same (j, j) continue; // The j column of temp is the Same as the j column of B.
ChangeL (I, j); // exchange the I, j columns of temp
Find = true;
Break;
}
}
If (find = false) // the corresponding column of the Column cannot be found.
{
Break;
}
}

If (find = true & need <ans)
Ans = need;
}

If (ans <inf)
Printf ("% d \ n", ans );
Else
Printf ("-1 \ n ");
}
Return 0;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.