2016.6.24

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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值