#include<iostream>
#include<map>
#include<queue>
#include<algorithm>
#include<utility>
using namespace std;
map<int, int> m;
queue<pair<int,int>> q;
void get_res()
{
while (!q.empty())
q.pop();
q.push(make_pair((1 << 16) - 1,0));
m.insert(make_pair((1 << 16) - 1,0));
while (!q.empty())
{
int c = q.front().first;
int n = q.front().second;
q.pop();
for (int i = 0;i < 16;i++)
{
int nc = c ^ (1 << i);
if (i + 4 < 16)
nc ^= 1 << (i + 4);
if (i - 4 > -1)
nc ^= 1 << (i - 4);
if (i % 4 > 0)
nc ^= 1 << (i - 1);
if (i % 4 < 3)
nc ^= 1 << (i + 1);
if (m.find(nc) == m.end())
{
m.insert(make_pair(nc, n + 1));
q.push(make_pair(nc, n + 1));
}
}
}
}
int main()
{
get_res();
int a = 0, b = 0;
for (int i = 0;i < 16;i++)
{
char c;
cin >> c;
if (c == 'b')
a += 1 << i;
else
b += 1 << i;
}
if (m.find(a) != m.end() || m.find(b) != m.end())
{
int ans1 = m[a];
int ans2 = m[b];
int ans = min(ans1, ans2);
cout << ans << endl;
}
else
cout << "Impossible" << endl;
return 0;
}
poj1753_Flip Game(广搜)
最新推荐文章于 2018-12-30 23:45:33 发布