#include <iostream> #include <queue> using namespace std; char _m[4][4]; bool mark[100000]; struct node { int step; int state; }; bool BFS(node p); bool boo; node t; void move(int r)//位操作的改变状态~ { t.state^=(1<<(15-r)); if(r>=4) t.state^=(1<<(15-r+4)); if(r<=11) t.state^=(1<<(15-r-4)); if((r%4)) t.state^=(1<<(15-r+1)); if(((r+1)%4)) t.state^=(1<<(15-r-1)); } queue<node> coll; int main() { //freopen("acm.acm","r",stdin); int i; int j; node begin; begin.state = 0; begin.step = 0; for(i = 0; i < 4; ++ i) { for(j = 0; j < 4; ++ j) { cin>>_m[i][j]; if(_m[i][j] == 'b') { begin.state += 1; } begin.state <<= 1; } } begin.state >>= 1; memset(mark,false,sizeof(mark)); if(begin.state == 0 || begin.state == 65535) { cout<<"0"<<endl; } else { coll.push(begin); mark[begin.state] = true; boo = false; while(!coll.empty() && !BFS(coll.front())) { coll.pop(); } if(!boo) { cout<<"Impossible"<<endl; } } } bool BFS(node p) { // cout<<p.state<<endl; if(p.state == 0 || p.state == 65535) { boo = true; cout<<p.step<<endl; return true; } int i; for(i = 0; i < 16; ++ i) { t = p; move(i); // cout<<t.state<<endl; if(!mark[t.state]) { t.step = p.step + 1; coll.push(t); mark[t.state] = true; } } return false; }
关注我的公众号,当然,如果你对Java, Scala, Python等技术经验,以及编程日记,感兴趣的话。
技术网站地址: vmfor.com