1.画家问题
复习复习复习!!!
首先比较关键的一点,如何枚举第一行。。。其实可以这么想
第一行假设有6列,就把它当做一个6位数的二进制数,那么完全枚举完所有情况就应该是遍历了从0 - 2^6 所有数,这样就得到了一个好的方法。
贴代码呀贴代码 ~
#include <iostream>
#include <cstring>
using namespace std;
int Wall[20][20];
int Paint[20][20];
int ans = 0;
int counts = 0;
void emunation(int k, int n)
{
int j = n;
while (j > 0)
{
Paint[1][j] = k % 2;
k /= 2;
j--;
}
}
int judge(int n)
{
for (int i = 2; i <= n; ++i)
{
for (int j = 1; j <= n; ++j)
{
Paint[i][j] = (Wall[i - 1][j] + Paint[i - 1][j] + Paint[i - 1][j - 1]
+ Paint[i - 1][j + 1] + Paint[i - 2][j]) % 2;
if (Paint[i][j] == 1)
counts++;
}
}
for (int j = 1; j <= n; ++j)
{
if (((Paint[n][j] + Paint[n][j - 1] + Paint[n][j + 1] + Wall[n][j] + Paint[n - 1][j]) % 2) != 0)
return 0;
}
if (counts < ans)
ans = counts;
return 1;
}
int main()
{
int n;
char wall[20][20];
cin >> n;
memset(Wall, 0, sizeof(Wall));
memset(Paint, 0, sizeof(Paint));
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j)
{
cin >> wall[i][j];
if (wall[i][j] == 'w')
Wall[i][j] = 1;
else
Wall[i][j] = 0;
}
ans = n * n + 1;
for (int j = 0; j < (1 << n); ++j)
{
counts = 0;
emunation(j, n);
for (int i = 1; i <= n; ++i)
if (Paint[1][i] == 1)
counts++;
judge(n);
}
if (ans != n * n + 1)
cout << ans << endl;
else
cout << "inf" << endl;
return 0;
}