#include <algorithm> #include <iostream> #include <cstring> #include <cstdlib> #include < cstdio> #include <cmath> #include <queue> using namespace std; int xx[5]={0,0,1,-1}, yy[5]={1,-1,0,0}; int Bg,ed,h,x,y;int t,w=1; BOOL ans[5][5],mark[100010]; Char ch[5]; struct data{bool a[5][5]; int step;} Q[100010]; int hash (bool a[5][5]) {int k=1,s=0; for (int i=1;i<=4;i++) for (int j=1;j<=4;j++) {s+=a[i][j]*k; k<<=1; } return s;} int main () {for (int i=1;i<=4;i++) {scanf ("%s", ch); for (int j=1;j<=4;j++) q[0].a[i][j]=ch[j-1]-' 0 '; } for (int i=1;i<=4;i++) {scanf ("%s", ch); for (int j=1;j<=4;j++) ans[i][j]=ch[j-1]-' 0 '; } bg=hash (Q[T].A); Ed=hash (ANS); if (bg==ed) {printf ("0"); return 0; } mark[bg]=1; while (T<W) {for (int i=1;i<=4;i++) for (int j=1; j<=4;j++) if (Q[t].a[i][j]) for (int k=0;k<4;k++) { X=xx[k]+i; Y=yy[k]+j; if (Q[t].a[x][y] | | | x>4 | | y>4 | | x<1 | | Y<1) continue; Swap (q[t].a[i][j],q[t].a[x][y]); H=hash (Q[T].A); if (!mark[h]) {if (h==ed) { printf ("%d", q[t].step+1); return 0; } mark[h]=1; memcpy (q[w].a,q[t].a,sizeof (Q[W].A)); q[w].step=q[t].step+1; w++; } swap (Q[t].a[i][j],q[t].a[x][y]); } t++; } return 0;}
BFs
"bzoj1054" [HAOI2008] Mobile Toys