C language: coin array problem _c language

Source: Internet
Author: User

There are m*n (M <=100,n <=100) gold coins lined up on the table in a M-row N-column array. Each gold coin or front face up or back upward. The status of the gold coins, 0 means the gold coin faces upward, 1 means the back facing up.

The rule of the Gold Coin array game is: (1) Each line of gold coins can be turned over and placed in the original position;

(2) Each can choose 2 columns, exchange the position of the 2 gold coins.

Programming Task: Given the initial state and target state of a coin array, the minimum number of transformations needed to transform the gold coin array from the initial state to the target State by programming the gold coin game rule.

Input

The input data has multiple sets of data. Line 1th has 1 positive integers k, which indicates a K-group of data. The 1th row of each set of data has 2 positive integers m and N. The M line below is the initial state of the gold coin array, with n digits representing the status of the gold coin, 0 for the gold coin facing up and 1 for the back facing up. The next m line is the target state of the coin array.

Output

The minimum number of transformations calculated is output in the order of the input data. Output-1 when the corresponding data is not solvable.

Code is someone else's, it feels good to write. Writing this blog, mainly want to revisit the idea.

Enumeration 1~m, where each column is listed in the first row,

From the 1~n line, find the rows that are not satisfied, and make a row transformation

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

At this point, the i=2 column (the second column) is compared to the column I of the target matrix.

If it is different, find out whether the J-column (= i+1~m) in this matrix is the same as that of the target matrix, if any, and the matrix is column J! = Target Matrix column J, then make a column transformation

If a qualifying column cannot be found, the first column enumerated is not likely to be transformed to the target matrix by the given rule.

Copy Code code as follows:

#include <stdio.h>

const int inf = 99999;
const int N = 101;

int a[n][n],b[n][n],temp[n][n]; A stores the initial matrix, B is the target State matrix
int n,m;
int need;//need to transform times

void Changel (int x,int y)/Transform 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)/transform line
{
int i;
for (i=1;i<=m;i++)
{
Temp[x][i]^=1;
}
}

BOOL Same (int x,int y)//Determine whether the column satisfies the condition
{
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 sets

while (tests--)
{
scanf ("%d%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; Ans Stores the final answer, the initial value is infinity


for (k=1;k<=m;k++)//enumeration columns listed in 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);


Rows that are not satisfied, make one transformation
for (i=1;i<=n;i++)
{
if (temp[i][1]!=b[i][1])//The row does not meet the condition
{
Changeh (i);//Transform Line
need++;
}
}

BOOL Find;
for (i=1;i<=m;i++)//Check whether each column satisfies the condition
{
Find=false;
if (Same (i,i))
{
Find=true;
Continue
}
for (j=i+1;j<=m;j++)//Find the same column in temp as I in B
{
The J of the IF (Same (i,j))//temp is the same as the I column in B
{
The J column of the IF (Same (j,j)) Continue;//temp is the same as the J column of B
Changel (I,J)//Exchange Temp i,j column
Find=true;
Break
}
}
if (find==false)//Does not find the column corresponding to the column
{
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.