翻转棋,枚举所有情况
加入一些位运算
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;
}