C-language gold coin Array Problem Solving method _c language

Source: Internet
Author: User

This article describes in detail the C language to achieve the problem of coin array solution, share for everyone to reference. The specific methods are as follows:

Problem Description:

There are M*n (1≤m, n≤100) coins lined up on the table into an array of m rows n columns. 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 rules of the coin array game are:

1. Each line of gold coins can be turned over and placed in the original position;
2. Each optional 2 column, exchange the position of the 2 gold coins.
In the case of the initial state and the target state of the given coin array, the minimum number of transformations required to transform the gold coin array from the initial state to the target State are programmed according to the gold coin game rules.

Data entry:

The first line of the input test data is a positive integer k of no more than 10, which indicates that there are k test cases. The first line of each test case is two positive integers m, n. Then the M row, each row has n 0 or 1 separated by whitespace. This m*n 0-1 represents the initial state array of a gold coin. The last is M line, each row n is 0 or 1 separated by a white space, representing the target state of the coin array.

Data output:

For each test case, the output line contains an integer that represents the minimum number of transformations required to transform the coin array from the initial state to the target State according to the requirements rule. Output 1 If the initial state cannot be converted to the target State (i.e., no solution) according to the transformation rule.

Sample data:

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

Sample Output
2
-1

The C language implementation code is as follows:

#include "stdio.h" #include "stdlib.h" #define SIZE int num; Enter several sets of data int row; Number of rows int column; Number of columns int count;
Number of exchanges int min; int a[size][size]; Initial matrix int b[size][size]; Final matrix int c[size][size]; temporary storage matrix int found;
  Initial to final whether there is an exchange void Trans_row (int x)//x row take counter {int i; for (i = 0;i<column; i++) b[x][i] = b[x][i]^1;
Vary or take the count++;
  } void Trans_column (int x, int y)//Exchange 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)//comparison x and Y columns 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 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);
    Protects the original array b min=row+column+1;  for (j=0;j<column;j++) {copy (b,c);//restore original array b count=0; Number of exchanges 0 trans_column (0,J); Each column is assumed to be the target state of the first column, and the case for (k=0;k<row;k++) {///If the row is different, convert the row to if (A[k][0]!=b[k][0]) Trans_row (k
     );
       for (k=0;k<column;k++) {//Poor all other columns, if the same exchange, indicating that the target State is uniform, otherwise can not find the same as the column, the description is not 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 feasible, find the minimum value min=count;
   if (min<row+column+1)//If the number of exchanges is smaller than the initial value, save it as the minimum number of exchanges for the current group, otherwise it is not possible to exchange exchgmin[i]=min;
  else Exchgmin[i]=-1;
  for (i=0;i<num;i++) printf ("%d/n", Exchgmin[i]);
  System ("pause"); ReTurn 0;

 }

I hope this article will help you with the learning of C program algorithm design.

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.