#include <iostream>
#include <queue>
using namespace std;
int visit[1<<16+10];
queue<int > Q;
int getnext(int temp, int x, int y){
temp ^= 1<<(4*x+y);
if (x-1>=0)
temp ^= 1<<(4*(x-1)+y);
if (x+1<4)
temp ^= 1<<(4*(x+1)+y);
if (y-1>=0)
temp ^= 1<<(4*x+y-1);
if (y+1<4)
temp ^= 1<<(4*x+y+1);
return temp;
}
int bfs(int state){
int step = 0;
Q.push(state);
while (!Q.empty()){
int t = Q.size();
for (int i = 0; i < t; ++i) { //这样做是为了控制层次
int temp = Q.front();
Q.pop();
if (temp == 0 || temp == 0xffff|| step>16)
return step;
for (int x = 0; x < 4; ++x) {
for (int y = 0; y < 4; ++y) {
int next = getnext(temp, x, y);
if (!visit[next]){
visit[next] = 1;
Q.push(next);
}
}
}
}
step++;
}
}
int main(){
// freopen("../in.txt", "r", stdin);
int start = 0;
int k = 0;
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4; ++j) {
char a;
cin>>a;
if (a == 'b')
start += (1<<k);
k++;
}
}
int ans = bfs(start);
if (ans>16)
cout<<"Impossible"<<endl;
else
cout<<ans<<endl;
}
POJ 1753
最新推荐文章于 2018-08-02 17:38:55 发布