C language gold coin array problem solution

  • 2020-04-02 02:45:10
  • OfStack

This article illustrates the solution to the problem of gold coin array in C language in detail. Specific methods are as follows:

Problem description:

We have m times n(1 Or less m, n Or less 100) gold pieces arranged in an array of 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:

1. Each row of gold COINS can be reversed and placed on the original position;
2. You can choose two columns at a time and swap the positions of the two gold COINS.
This problem requires that for the given initial state and target state of the gold coin array, the minimum number of transformations required to transform the gold coin array from the initial state to the target state is calculated programmatically according to the rules of the gold coin game.

Data input:

The first row of the input test data is a positive integer k not exceeding 10, indicating that there are k test cases. Finally, there are m rows, n blank delimited zeros or ones in each row, representing the target state of the gold coin array.

Data output:

For each test case, the output line contains an integer representing the minimum number of transformations required to transform the gold coin array from its original state to the target state according to the required rule. If the initial state cannot be transformed to the target state (that is, when there is no solution) according to the transformation rule, the output -1.

Data sample:

The Sample Input
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

The Sample Output
2
- 1

C language implementation code is as follows:


#include "stdio.h"
#include "stdlib.h"
#define size 100
int num; //Enter several sets of data
int row; //The number of rows
int column; //The number of columns
int count; //Switching frequency
int min;
int a[size][size]; //The initial matrix
int b[size][size]; //Eventually matrix
int c[size][size]; //Temporary storage matrix
int found; //Whether there is an exchange from the beginning to the end
void trans_row(int x) //The x row is inverted
{
  int i;
  for (i = 0;i<column; i++) 
    b[x][i] = b[x][i]^1; //Exclusive or invert
  count++;
}
void trans_column(int x, int y) //Swap the x and y columns
{
  int i;
  int temp;
  for(i = 0; i < row; i++){
   temp=b[i][x];
   b[i][x]=b[i][y];
   b[i][y]=temp;
  }
  if (x != y) 
   count++;
}
int is_same(int x, int y) //Compare the x and y columns to see if they are the same
{
  int i;
  for(i = 0; i <row; i++)
    if (a[i][x] != b[i][y])
      return 0;
  return 1;
}
void copy(int a[size][size], int b[size][size]) //Copy an array
{
  int i,j;
  for (i = 0; i <row; i++)
   for (j = 0; j < column; j++)
     a[i][j] = b[i][j];
}
int main(){
  int i,j,k,p;
  int exchgmin[size];
  scanf("%d",&num);
  for(i=0;i<num;i++){
    scanf("%d",&row);
    scanf("%d",&column);
    for(j=0;j<row;j++)
     for(k=0;k<column;k++)
      scanf("%d",&a[j][k]);
    for(j=0;j<row;j++)
     for(k=0;k<column;k++)
      scanf("%d",&b[j][k]);
    copy(c,b); //Protect the original array b
    min=row+column+1;
    for(j=0;j<column;j++){
     copy(b,c); //Restore the original array b
     count=0;  //Switching frequency reset  
     trans_column(0,j); //Each column is assumed to be the target state for the first column, exhaustive for the column
     for(k=0;k<row;k++){ //If the rows are different, the rows are converted
      if(a[k][0]!=b[k][0])
       trans_row(k);
     }
     for(k=0;k<column;k++){//If all other columns are exhaustive, if they are the same, they are swapped, indicating that the target state is uniform. Otherwise, if the same column is not found, it is not feasible
       found=0;
       for(p=k;p<column;p++){
        if(is_same(k,p)){
         trans_column(k,p);
         found=1;
         break;
        }
       }
       if(!found)
        break;
     }
     if(found&&count<min) //If possible, find the minimum
       min=count; 
    }
   if(min<row+column+1) // if Switching frequency Smaller than the initial value, save it to the minimum of the current group Switching frequency , otherwise the exchange cannot be achieved  
     exchgmin[i]=min;
   else exchgmin[i]=-1;
  }
  for(i=0;i<num;i++)
   printf("%d/n",exchgmin[i]);
  system("pause");
  return 0;
}

I hope that this paper is helpful to the learning of C programming algorithm design.


Related articles: