金币阵列问题

#include "stdio.h"
#include "string.h"
#include "fstream"
#include "iostream"
using namespace std;
ifstream fin("coins.txt");

int m, n; //m行,n列
const int N = 10;
int temp[N][N]; 
int from[N][N];
int to[N][N];

bool same(int x, int y) //判断temp第x列是否与to第y列相同
{
    int i;
    for(i=0; i<m; i++)
        if(temp[i][x] != to[i][y])
            return false;
    return true;
}

void trans1(int x)  //变换temp第x行
{
    int i;
    for(i=0; i<n; i++)
        temp[x][i] ^= 1;
}

void swap(int &a, int &b)
{
    int temp = a;
    a = b;
    b = temp;
}

void trans2(int x, int y) //交换temp第x列与第y列
{
    int i;
    for(i=0; i<m; i++)
        swap(temp[i][x], temp[i][y]);
}

void acopy() //将from拷贝到temp中
{
    int i, j;
    for(i=0; i<m; i++)
        for(j=0; j<n; j++)
            temp[i][j] = from[i][j];
}

int main()
{
    int k;
    printf("输入数据组数:");
    fin >> k;
    cout << k;
    while(k--)
    {
        /////////////////数据初始化/////////////////
        printf("\n输入行数:");
        fin >> m;
        cout << m;
        printf("\n输入列数:");
        fin >> n;
        cout << n;
        printf("\n输入原始状态矩阵:\n");
        int i, j;
        for(i=0; i<m; i++)
        {
            for(j=0; j<n; j++)
            {
                fin >> from[i][j];
                cout << from[i][j] << " ";
            }
            cout << endl;
        }
        printf("输入目标状态矩阵:\n");
        for(i=0; i<m; i++)
        {
            for(j=0; j<n; j++)
            {
                fin >> to[i][j];
                cout << to[i][j] << " ";
            }
            cout << endl;
        }

        /////////////////进行计算的主体部分/////////////////
        int change; 
        int best = m + n + 1;  //最少的变换次数
        for(j=0; j<n; j++) //枚举每一列为第一列
        {
            acopy();  //将原始状态矩阵拷贝到临时矩阵中
            change = 0;  //需要变换的次数
            trans2(0, j); 
            if(j!=0)
                change++;
            for(i=0; i<m; i++)  //将临时矩阵与目标状态第一列中不一样的数所在行进行变换
                if(temp[i][0] != to[i][0])
                {
                    trans1(i);
                    change++;
                }
            bool find;
            for(i=0; i<n; i++)  //对于目标状态中的每一列,看是否能找到temp中某一列与之相同,
            {                   //如果能找到,则只需要再进行列交换即可得到目标状态
                find = false;
                int s;
                for(s=i; s<n; s++)
                {
                    if(same(s, i)) //如果临时矩阵第s列与目标状态第i列相同
                    {
                        if(s != i) //如果与目标状态对应列位置不同,进行交换,否则不需要交换
                        {
                            trans2(i, s); //交换临时矩阵第i列与第s列
                            change++;
                        }
                        find = true;
                        break;
                    }
                }
                if(!find)
                    break;
            } 
            if(change<best  && find)
                    best = change;
        }//列枚举结束
        if(best<m+n+1)
            printf("最少的变换次数为:%d\n", change);
        else
            printf("原状态不能变到目标状态!\n");
    }//while结束
    fin.close();
    return 0;
}

这里写图片描述
coins.txt
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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值