# 金币阵列问题

``````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``````

``````2
-1``````

``````#include <fstream>
#include <iostream>
using namespace std;
const int size = 100;
int k,n,m,ccount,best;
int b0[size+1][size+1],b1[size+1][size+1],b[size+1][size+1];
bool found;
void print()
{
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= m; j++)
cout << b1[i][j] << " ";
cout << endl;
}
}

void trans1(int x)
{
for(int i = 1; i <= m; i++)
b1[x][i] = b1[x][i]^1;
ccount++;
}
void trans2(int x, int y)
{
for(int i = 1; i <= n; i++)
swap(b1[i][x],b1[i][y]);
if (x != y) ccount++;
}
bool same(int x, int y)
{
for(int i = 1; i <= n; i++)
if (b0[i][x] != b1[i][y])
return false;
return true;
}
void acpy(int a[size+1][size+1], int b[size+1][size+1])
{
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
a[i][j] = b[i][j];
}
{
int x,y,j,p;
cin >> k;
for(int i = 1; i <= k; i++)
{
cin >> n >> m;
for( x = 1; x <= n; x++)
for( y = 1; y <= m; y++)
cin >> b0[x][y];
for( x = 1; x <= n; x++)
for(int y = 1; y <= m; y++)
cin >> b1[x][y];
acpy(b,b1);
best = m + n + 1;
for( j = 1; j <= m; j++)
{
acpy(b1,b);
ccount = 0;
trans2(1,j);
int p;
for( p = 1; p <= n; p++)
if (b0[p][1] != b1[p][1])
trans1(p);
for(p = 1; p <= m; p++)
{
found = false;
for(int q = p; q <= m; q++)
if (same(p,q))
{
trans2(p,q);
found = true;
break;
}
if (!found) break;
}

if (found && ccount < best)
best = ccount;
}
if (best < m + n + 1)
cout << best << endl;
else
cout << -1 << endl;
}
}
int main()
{
return 0;
}

``````

``````#include <fstream>
#include <iostream>
using namespace std;
const int size = 100;
int k,n,m,ccount,best;
int b0[size+1][size+1],b1[size+1][size+1],b[size+1][size+1];
bool found;
void print()
{
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= m; j++)
cout << b1[i][j] << " ";
cout << endl;
}
}

void trans1(int x)
{
for(int i = 1; i <= m; i++)
b1[x][i] = b1[x][i]^1;
ccount++;
}
void trans2(int x, int y)
{
for(int i = 1; i <= n; i++)
swap(b1[i][x],b1[i][y]);
if (x != y) ccount++;
}
bool same(int x, int y)
{
for(int i = 1; i <= n; i++)
if (b0[i][x] != b1[i][y])
return false;
return true;
}
void acpy(int a[size+1][size+1], int b[size+1][size+1])
{
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
a[i][j] = b[i][j];
}
{
int x,y,j,p;
cin >> k;
for(int i = 1; i <= k; i++)
{
cin >> n >> m;
for( x = 1; x <= n; x++)
for( y = 1; y <= m; y++)
cin >> b0[x][y];
for( x = 1; x <= n; x++)
for(int y = 1; y <= m; y++)
cin >> b1[x][y];
acpy(b,b1);
best = m + n + 1;
for( j = 1; j <= m; j++)
{
acpy(b1,b);
ccount = 0;
trans2(1,j);
int p;
for( p = 1; p <= n; p++)
if (b0[p][1] != b1[p][1])
trans1(p);
for(p = 1; p <= m; p++)
{
found = false;
for(int q = p; q <= m; q++)
if (same(p,q))
{
trans2(p,q);
found = true;
break;
}
if (!found) break;
}

if (found && ccount < best)
best = ccount;
}
if (best < m + n + 1)
cout << best << endl;
else
cout << -1 << endl;
}
}
int main()
{
return 0;
}

``````