Coin array Problem

Source: Internet
Author: User

The topic is not described, see Computer algorithm design and Analysis Exercise Solution (second edition) Wang Xiaodong 6th page

/*
* CoinSort.cpp
*
* Created On:dec 4, 2014
* Author:hujianjie
*/
#include <iostream>
using namespace Std;
int Max = 10;
int N, m, Count, best;
BOOL Flag;
int arrold[10][10];
int arrnew[10][10];
int arrmid[10][10];

void copy (int arr1[10][10], int arr2[10][10]) {
for (int i = 0; i < n; i++) {
for (int j = 0; J < m; J + +) {
ARR1[I][J] = Arr2[i][j];
}
}
}
void Transrow (int row) {
for (int i = 0; i < m; i++) {
Arrnew[row][i] = arrnew[row][i] ^ 1;
Arrold[row][i] = arrold[row][i] ^ 1;
}
cout << "Transrow" << Endl;
count++;

}
void Transcolum (int mid, int tem) {
int temp;
for (int k = 0; k < n; k++) {
temp = Arrnew[k][mid];
Arrnew[k][mid] = Arrnew[k][tem];
Arrnew[k][tem] = temp;
temp = Arrold[k][mid];
Arrold[k][mid] = Arrold[k][tem];
Arrold[k][tem] = temp;
}
cout << "Transcolum" << Endl;
count++;

}
BOOL Issame (int mid, int tem) {
for (int k = 0; k < n; k++) {
if (Arrold[k][tem]! = Arrnew[k][mid]) {
return false;
}
}
return true;
}

int main () {
Flag = false;
Count = 0;
cout << "Please input row N and colum m!" << Endl;
CIN >> n >> m;
cout << "Please input old!" << Endl;
for (int i = 0; i < n; i++) {
for (int j = 0; J < m; J + +) {
CIN >> Arrold[i][j];
}
}
cout << "Please input New!" << Endl;
for (int i = 0; i < n; i++) {
for (int j = 0; J < m; J + +) {
CIN >> Arrnew[i][j];
}
}
Copy (Arrmid, arrnew);
Copy (Arrmid, arrold);
Best = n + M + 1;
for (int colum = 0; colum < m; colum++) {
cout << colum << Endl;
Copy (Arrnew,arrmid);
Copy (Arrold, arrmid);
Count = 0;
Convert each column to the first column
if (Colum! = 0)
Transcolum (0, Colum);
Compares each row of the first column, and the difference is reversed
for (int row = 0; row < n; row++) {
if (arrnew[row][0]! = Arrold[row][0]) {
Transrow (row);
}
}
After the first row is determined and flipped, compare the remaining columns
for (int mid = 1; mid < m; mid++) {
for (int tem = MID; tem < m; tem++) {
if (Issame (Mid, tem)) {
if (Mid! = TEM)
Transcolum (Mid, TEM);
Flag = true;

} else {
Flag = false;
}
}
}
if (flag = = True && count < best)
Best = count;
}
if (Best < (m + n + 1))
cout << "The best Steps is:" << best << Endl;
Else
cout << "Can ' t finished!" << Endl;
return 0;
}
/*
1 0 1
0 0 0
1 1 0
1 0 1
*
*
1 0 1
1 1 1
0 1 1
1 0 1
*
*
*
*
*/
C language is not good to write, the idea is: each column of the initial matrix to try as the first column of the target, according to the idea of enumerating a column of the comparison

Coin array Problem

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.