题意:给出一个二维数组,由0和1组成,维数不超过15,元素的上下左右四个方向的元素中1的个数和来表示元素的奇偶性,求经过几次将0转为1的变换,使得每个元素都表现为偶性
思路:如果穷举二维数组中的每个元素,显然不现实。 用Ep(r,c)表示第r行第c列的元素的奇偶性,可以从第一行开始穷举,然后通过元素的奇偶性可以知道 Ep(r,c) = a[r - 1][c] + a[r][c-1]+a[r][c+1]+a[r+1][c],通过这个关系式可以知道a[r+1][c] =Ep(r,c)-a[r-1][c] - a[r][c-1]-a[r][c+1],因为Ep(r,c)为偶数,所以a[r-1][c]+a[r][c-1]+a[r][c+1]与a[r+1][c]要么都是奇数,要么都是偶数。
具体代码如下:
#include <iostream>
#include <fstream>
#include <climits>
#include <algorithm>
using namespace std;
int** newA(int n);
void freeA(int **a, int n);
void print(int**a, int n)
{
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < n; ++j)
{
cout << a[i][j] << " ";
}
cout << endl;
}
}
class Solution
{
public:
Solution(int **a, int n)
{
m_pa = a;
m_n = n;
}
int solve()
{
int** b = newA(m_n);
int ans = INT_MAX;
for (int i = 0; i < (1 << m_n); ++i)
{
int tmp = check(b, i);
ans = min(ans, tmp);
}
freeA(b, m_n);
return ans;
}
private:
int check(int** b, int s)
{
for (int i = 0; i < m_n; ++i)
{
if (s & (1 << (m_n - 1 - i)))
{
b[0][i] = 1;
}
else if (1 == m_pa[0][i])
{
return INT_MAX;
}
else
{
b[0][i] = 0;
}
}
for (int r = 1; r < m_n; ++r)
{
for (int c = 0; c < m_n; ++c)
{
int sum = 0;
if (r > 1) sum += b[r - 2][c];
if (c > 0) sum += b[r - 1][c - 1];
if (c < m_n - 1) sum += b[r - 1][c + 1];
b[r][c] = sum % 2;
if (0 == b[r][c] && 1 == m_pa[r][c]) return INT_MAX;
}
}
int ans = 0;
for (int i = 0; i < m_n; ++i)
{
for (int j = 0; j < m_n; ++j)
{
if (b[i][j] != m_pa[i][j]) ++ans;
}
}
return ans;
}
private:
int** m_pa;
int m_n;
};
int** newA(int n)
{
int**a = new int*[n];
for (int i = 0; i < n; ++i)
{
a[i] = new int[n];
}
return a;
}
void freeA(int **a, int n)
{
for (int i = 0; i < n; ++i)
{
delete[] a[i];
}
delete[] a;
}
int main()
{
#ifndef ONLINE_JUDGE
ifstream fin("f:\\OJ\\uva_in.txt");
streambuf *old = cin.rdbuf(fin.rdbuf());
#endif
int t;
int n;
cin >> t;
for (int i = 0; i < t; ++i)
{
cin >> n;
int** a = newA(n);
for (int j = 0; j < n; ++j)
{
for (int k = 0; k < n; ++k)
{
cin >> a[j][k];
}
}
Solution solver(a, n);
int ans = solver.solve();
freeA(a, n);
cout << "Case " << i + 1 << ": ";
if (INT_MAX == ans)
{
cout << -1 << endl;
}
else
{
cout << ans << endl;
}
}
#ifndef ONLINE_JUDGE
cin.rdbuf(old);
#endif
return 0;
}