[1-4] array issue coins

Problem Description:
There m gold arrays' n (m £ 100, n £ 100) coins arranged on the desktop m rows and n columns. Each a gold
coin or face up or back up. It represents a state with the number of gold, gold 0 up a front, the back up.
Gold the rules of the game of the array is:
(1) each can be placed in any row gold turn over the original position;
(2) optionally two each, these two switching positions gold.
'Programming tasks:
to set the initial and target states of the array of gold, gold the rules of the game program is calculated according to the array of gold from an initial state
transformed state to the target state required minimum number of transitions.
'Input Data:
given by the input data file input.txt. File more than one set of data. Line 1 of the first file positive integer k, the table
shown have k sets of data. Each of the first data row has two positive integers m and n. The following is the initial line-shaped coins m array
state, each row having n digital line shows a state of the gold, gold 0 up a front, the back up. Then the
m rows of the array are the target state gold.
'Output Results:
The number of times the calculated minimal conversion to the output file in the order of input data output.txt. When no solution corresponding data
outputs -1.
Input sample output sample files
input.txt output.txt
2
. 4. 3
. 1. 1 0
0 0 0
. 1 0. 1
. 1. 1 0
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
2
-1

【answer】


We assume that the final answer in the first column of a j-th column in the array and the array b is the same as
the first row so we will first negate operation with the line really put a j-th column array into an array and b same.
This time you will find that you can no longer negate operation with the line.
Only the following exchange operation can be used.
Then just put into a array of arrays and b n column vectors (represented by a string), and the two rows of arrays are in accordance with a string like sequence (string lexicographic order).
If the two string array identical, then it can be changed from a to b
i th column b want to change to the first columns of a record on it.
Then it becomes another problem: the
numbers on the last known location to the Next i [i] position. Exchange operation can only ask for a minimum of several cycles to meet all i (1 <= i <= n)
the classic problem, eventually forming a plurality of rings, [Sigma (ring size of -1) is Procedure a number of times before the negate operation is coupled in the same situation a j-th column and the first column answer b

[Code]

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;

struct abc{
    string s;
    int column;
};

const int N = 100;

int T,m,n;
int a[N+10][N+10],b[N+10][N+10];
abc bcs[N+10];
int aa[N+10][N+10];
int minstep = -1;

bool cmp(abc a,abc b){
    return a.s<b.s;
}

void fz(int r){
    for (int i = 1;i <= n;i++){
        aa[r][i] = 1-aa[r][i];
    }
}



int judge(int idx){

    int step = 0;
    for (int i = 1;i <= m;i++){
        if (aa[i][idx]!=b[i][1]){
            fz(i);
            step++;
        }
    }
    //把aa的每一列看成一个长度为m的01字符串,即n个字符串
    abc acs[N+10];
    int nex[N+10];//记录aa的每一列需要变换到哪一列
    for (int j = 1;j <= n;j++){
        string temp = "";
        for (int i = 1;i <= m;i++){
            temp = temp + (char)(aa[i][j]+'0');
        }
        acs[j].column = j;
        acs[j].s = temp;
    }
    sort(acs+1,acs+1+n,cmp);
    //for (int i = 1;i <= n;i++) cout<<acs[i].s<<" ";cout<<endl;
    //for (int i = 1;i <= n;i++) cout<<bcs[i].s<<" ";
    //exit(0);
    for (int i = 1;i <= n;i++)
        if (acs[i].s!=bcs[i].s)
            return -1;
        else {
            nex[acs[i].column] = bcs[i].column;
        }
    int flag[N+10];
    for (int i = 1;i <= n;i++) flag[i] = -1;
    for (int i = 1;i <= n;i++)
        if (flag[i]==-1){
            int j = i;
            int cnt = 0;
            while (flag[j]==-1){
                flag[j] = 1;
                cnt++;
                j = nex[j];
            }
            step+=cnt-1;
        }
    return step;
}

int main(){
    //freopen("E://tran5.in","r",stdin);
    cin >> T;
    while (T--){
        minstep = -1;
        cin >> m >> n;
        for(int i = 1;i <= m;i++)
            for (int j = 1;j <= n;j++)
                cin >> a[i][j];
        for(int i = 1;i <= m;i++)
            for (int j = 1;j <= n;j++)
                cin >> b[i][j];
        //将b转换成n个列向量
        for (int j = 1;j <= n;j++){
            string temp="";
            for (int i = 1;i <= m;i++){
                temp = temp + (char)(b[i][j]+'0');
            }
            bcs[j].s = temp;
            bcs[j].column = j;
        }
        sort(bcs+1,bcs+1+n,cmp);

        for (int j = 1;j <= n;j++){//suppose j column equal to first column of b
            memcpy(aa,a,sizeof(a));
            int t = judge(j);
            if (t==-1) continue;
            if (minstep==-1){
                minstep = t;
            }else minstep = min(minstep,t);
        }
        cout<<minstep<<endl;
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/AWCXV/p/11616757.html
1-4