poj1753

翻转棋,枚举所有情况
加入一些位运算
x^1可以实现0变1,1变0
x&1可以取最后一位
0-65535循环一遍

//
//  main.cpp
//  poj1753
//
//  Created by 耿飞 on 15/8/12.
//  Copyright (c) 2015年 耿飞. All rights reserved.
//

#include <iostream>
#include <cstdio>
using namespace std;
bool map[6][6];
int tx[4]={0,1,0,-1};
int ty[4]={1,0,-1,0};
bool f[17];
bool check()
{
    int ty=map[1][1];
    for (int i=1;i<=4;i++)
        for (int j=1;j<=4;j++)
            if (map[i][j]!=ty)
                return false;
    return true;
}
void change()
{
    for (int i=1;i<=16;i++)
    {
        if (f[i])
        {
            int x=(i+3)/4;
            int y=i+4-4*x;
            map[x][y]=map[x][y]^1;
            for (int j=0;j<4;j++)
                map[x+tx[j]][y+ty[j]]=map[x+tx[j]][y+ty[j]]^1;
        }
    }
}
int main(int argc, const char * argv[])
{
    int i,j;
    char k[10];
    int ans,cnt;
    i=1;ans=99999;
    for (i=1;i<=4;i++)
    {
        scanf("%s",k);
        for (j=0;j<4;j++)
            if (k[j]=='w')
                map[i][j+1]=1;
            else
                map[i][j+1]=0;
    }
    for (i=0;i<=65535;i++)
    {
        cnt=0;
        for (j=0;j<16;j++)
        {
            f[j+1]=i>>j&1;
            cnt+=f[j+1];
        }
        change();
        if (check())
            ans=min(ans,cnt);
        change();
    }
    if (ans==99999)
        cout<<"Impossible"<<endl;
    else
        cout<<ans<<endl;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值