C language: gold coin array problem

  • 2020-04-01 21:32:24
  • OfStack

Have m * n (m < = 100, n < =100) an array of gold COINS in m rows and n columns on the table. Each gold coin is either heads or tails. I'm going to put a number on it, zero on it, and one on it.

The rules of the gold coin array game are:

(2) you can choose 2 columns at a time and swap the positions of these 2 gold pieces.

Programming task: given the initial state and target state of the gold coin array, programming calculation according to the rules of the gold coin game, the minimum number of transformations required to transform the gold coin array from the initial state to the target state.

The Input

The input data has multiple sets of data. The first row has a positive integer k, indicating that there are k sets of data. The first row of each set of data has two positive integers m and n. The following m rows are the initial state of the gold coin array. Each row has n Numbers representing the state of the gold coin, with 0 representing the gold coin facing up and 1 representing the gold coin facing up. The next m row is the target state of the gold coin array.

The Output

Output the calculated minimum number of transformations in the order of the input data. Output -1 when corresponding data has no solution.

The code is someone else's and feels well written. I wrote this blog mainly to refresh my mind.

Enumerate the case of the first column of each column in 1~m,

// from 1~n rows, find the unsatisfied rows and perform a row operation

// if the enumerated column is successfully converted to the target matrix according to the rule, the difference between the matrix and the original matrix is only in the column order

At this point, starting from column I =2 (the second column), compare with the ith column of the target matrix,

If not, find whether the JTH column in this matrix (= I +1~m) is the same as the ith column of the target matrix, if so, and the JTH column of this matrix! = the JTH column of the target matrix, then, carry out a column transformation

// it is impossible to transform the first column of the enumerated column to the target matrix according to the given rule if no qualified column can be found


#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 and b the target state matrix
 int n,m;
 int need;//Number of transformations required

 void ChangeL(int x,int y)//Transformation 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 sets of data

     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 with an initial value of infinity

 
             for(k=1;k<=m;k++)//Enumerate the first column of each column
             {
                 for(i=1;i<=n;i++)
                     for(j=1;j<=m;j++)
                         temp[i][j]=a[i][j];
                 need=0;
                 ChangeL(1,k);

 
                 //I'm going to do a transformation for the row that doesn't satisfy
                 for(i=1;i<=n;i++)
                 {
                     if(temp[i][1]!=b[i][1])//The row does not satisfy the condition
                     {
                         ChangeH(i);//Transform line
                         need++;
                     }
                 }

                 bool find;
                 for(i=1;i<=m;i++)//Check that each column satisfies the condition
                 {
                     find=false;
                     if(Same(i,i))
                     {
                         find=true;
                         continue;
                     }
                     for(j=i+1;j<=m;j++)//Look for the same column in temp as the I column of b
                     {
                         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);//I, j columns of temp
                             find=true;
                             break;
                         }
                     }
                     if(find==false)//The column could not be found
                     {
                         break;
                     }
                 }

                 if(find==true&&need<ans)
                     ans=need;
             }

             if(ans<inf)
                 printf("%dn",ans);
             else
                 printf("-1n");
     }
     return 0;
 }


Related articles: