搜索 金币阵列问题

金币阵列问题

问题描述:

有m x n (m<=100, n<=100 ) 个金币在桌面上排成一个m行n 列的金币阵列。每一枚金币或正面朝上或背面朝上。用数字表示金币状态,0表示金币正面朝上,1 表示背面朝上。金币阵列游戏的规则是:

  1. 每次可将任一行金币翻过来放在原来的位置上;
  2. 每次可任选2 列,交换这2 列金币的位置。

算法设计:

给定金币阵列的初始状态和目标状态,计算按金币游戏规则,将金币阵列从初始状态变换到目标状态所需的最少变换次数。

数据输入:
由文件input.txt给出输入数据。文件中有多组数据。文件的第1行有1 个正整数k,表示有k 组数据。每组数据的第1 行有2 个正整数m 和n。以下的m行是金币阵列的初始状态,每行有n 个数字表示该行金币的状态,0 表示金币正面朝上,1 表示背面朝上。接着的m行是金币阵列的目标状态。

结果输出:

将计算出的最少变换次数按照输入数据的次序输出到文件output.txt。相应数据无解时输出-1。

输入文件示例 输出文件示例
input.txt

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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

output.txt

1
2
3
2

-1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int INF = 0x3f3f3f3f;
typedef long long LL;
const int maxn = 15;
int m, n;
int src[maxn][maxn], des[maxn][maxn];
int cnt, tmp[maxn][maxn];

void copy(int a[maxn][maxn], int b[maxn][maxn])
{
for (int i = 1; i <= m; ++i)
for (int j = 1; j <= n; ++j)
a[i][j] = b[i][j];
}

void flipByRow(int r)
{ //对某一行进行翻转
for (int j = 1; j <= n; ++j)
tmp[r][j] = tmp[r][j] ^ 1; // a[j][i]^=1; //异或运算也可以
++cnt;
}

void exchangeByColumn(int c1, int c2)
{ //对两列进行互换
for (int i = 1; i <= m; ++i)
swap(tmp[i][c1], tmp[i][c2]);
if (c1 != c2)
++cnt;
}

bool same(int c1, int c2)
{
//判断两列是否相同
for (int i = 1; i <= m; ++i)
if (des[i][c1] != tmp[i][c2])
return false;
return true;
}

int main()
{
int T;
cin >> T;
while (T--)
{
cin >> m >> n;
for (int i = 1; i <= m; ++i)
for (int j = 1; j <= n; ++j)
cin >> src[i][j];
for (int i = 1; i <= m; ++i)
for (int j = 1; j <= n; ++j)
cin >> des[i][j];

int best = m + n + 1;

//整体思路,将每一列运用列交换作为第1列,然后对每一行进行判断,如果元素//与目标元素不相等,

//进行翻转处理,再向后判断相关列是否相同。
for (int col = 1; col <= n; ++col)
{ //在tmp中依次寻找des的每一列
copy(tmp, src); //先复制数组
cnt = 0;
exchangeByColumn(1, col); //将第k列与第1列互换
//对所有行的第1个元素与目标数组对应的元素进行比较,如果不同
//进行按行变换
for (int i = 1; i <= m; ++i)
if (tmp[i][1] != des[i][1])
flipByRow(i);
//检查每一列是否满足条件
bool found;
for (int c1 = 1; c1 <= n; ++c1)
{ //判断是否可以通过后续列的交换达到目的
found = false;
for (int c2 = c1; c2 <= n; ++c2)
if (same(c1, c2))
{
exchangeByColumn(c1, c2);
found = true;
break;
}
if (!found)
break;
}
if (found && best > cnt)
best = cnt;
}

if (best < m + n + 1)
cout << best << "\n";
else
cout << "-1\n";
}
return 0;
}