It can be hashed, but lazy to write a hash, so slow to death.
Code:
1#include <cstdio>2#include <queue>3#include <Set>4#include <algorithm>5 using namespacestd;6 Const intdi[]={0,0,-1,1},dj[]={-1,1,0,0};7 structsqu{Chars[4][4];};8 structNode{squ v;intD Node (ConstSqu &a,Const int&B) {v=a;d=b;} Node () {}};9 BOOL operator< (ConstSqu &a,ConstSqu &b)Ten { One for(intI=0;i<4; i++) A for(intj=0;j<4; j + +) - if(a.s[i][j]<B.s[i][j]) - return true; the Else if(a.s[i][j]>B.s[i][j]) - return false; - return false; - } + BOOL operator== (ConstSqu &a,ConstSqu &b) - { + for(intI=0;i<4; i++) A for(intj=0;j<4; j + +) at if(a.s[i][j]!=B.s[i][j]) - return false; - return true; - } -Squ from, to; - Set<Squ>Set; inQueue<node>Q; -InlineBOOLCheckConst int&x,Const int&y) to { + if(x>=0&&x<4&&y>=0&&y<4&&q.front (). v.s[x][y]=='0') - return true; the return false; * } $InlinevoidWorkConstSqu &tmp)Panax Notoginseng { - if(Set.find (tmp) = =set.end ()) the { + if(tmp==to ) A { theprintf"%d\n", Q.front (). d+1); +Exit0); - } $ Set.insert (TMP); $Q.push (Node (Tmp,q.front (). d+1)); - } - } the intMain () - {Wuyi for(intI=0;i<4; i++) scanf ("%s", from. S[i]); the for(intI=0;i<4; i++) scanf ("%s", to. S[i]); - if( from==to) {printf ("0\n");return 0;} WuQ.push (Node ( from,0)); -Set.insert ( from); About while(!q.empty ()) $ { - for(intI=0;i<4; i++) - for(intj=0;j<4; j + +) - if(Q.front (). v.s[i][j]=='1') A { + for(intk=0;k<4; k++) the { - intti=i+di[k],tj=j+Dj[k]; $ if(check (TI,TJ)) the { theSqu tmp=Q.front (). V; the swap (tmp. S[i][j],tmp. S[TI][TJ]); the Work (TMP); - } in } the } the Q.pop (); About } the return 0; the}
"BFS" bzoj1054 [HAOI2008] Mobile Toys