#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<cstdio>
#include <string>
#define ll long long
using namespace std;
int st,vis[(1 << 17)];
int change(int x,int w)
{
x ^= (1 << w);
if(w > 3)
{
x ^= (1 << (w - 4));
}
if(w < 12)
{
x ^= (1 << (w + 4));
}
if(w % 4)
{
x ^= (1 << (w - 1));
}
if(w % 4 < 3)
{
x ^= (1 << (w + 1));
}
return x;
}
int bfs()
{
queue<pair<int,int> >q;
pair<int,int>stt;
stt.first = st,stt.second = 0;
//cout << st << endl;
q.push(stt);
vis[st] = 1;
if(st == 0 || st == 65535)
return 0;
while(!q.empty())
{
pair<int,int> te = q.front();
q.pop();
for(int i = 0;i < 16;i++)
{
int now = change(te.first,i);
if(now == 0 || now == 65535)
return te.second + 1;
if(vis[now])
continue;
vis[now] = 1;
q.push(make_pair(now,te.second + 1));
}
}
return -1;
}
int main(){
string gg;
while(cin >> gg)
{
memset(vis,0,sizeof(vis));
st = 0;
for(int j = 0;j < 4;j++)
{
if(gg[j] == 'b')
{
st |= (1 << (j));
}
}
for(int i = 1;i < 4;i++)
{
char s[11];
scanf("%s",s);
for(int j = 0;j < 4;j++)
{
if(s[j] == 'b')
{
st |= (1 << (i * 4 + j));
}
}
}
//cout << st << endl;
int ou = bfs();
if(ou == -1)
cout << "Impossible\n";
else
cout << ou << endl;
}
return 0;
}
POJ 1753 Flip Game
最新推荐文章于 2016-11-27 22:45:08 发布