8 Realm: http://www.cnblogs.com/goodness/archive/2010/05/04/1727141.html
Realm one, violence wide search +stl (HDU memory overrun, POJ time overrun)
Map storage path, set the weight, the string stored state, no doubt, fried.
#include <cstdio>#include<cstring>#include<string>#include<cmath>#include<vector>#include<queue>#include<Set>#include<map>#include<algorithm>#include<iostream>using namespacestd;Charinput[ +];intdir[4][2] = { { -1,0},{1,0},{0,-1},{0,1 } };stringD ="Durl";Set<string>F;map<string,string>m;intSZ =0;structnode{strings; stringpath; intPOS; Node () {} node (stringStrstringPaintPos) {s=str; Path=PA; POS=Pos; }};BOOLGintAintb) { if(A >=0&& a <=2&& b >=0&& b <=2)return 1; return 0;}voidPre () {queue<node>Q; Q.push (Node ("12345678x","",8)); m["12345678x"] =""; F.insert ("12345678x"); while(!Q.empty ()) {node H=Q.front (); Q.pop (); intA = H.pos/3, B = h.pos%3; for(inti =0; i<4; i++) { intx = a + dir[i][0], y = B + dir[i][1]; if(!g (x, y))Continue; intpos =3* x +y; Swap (H.s[h.pos], H.s[pos]); if(F.find (H.S)! =F.end ()) {Swap (H.s[h.pos], H.s[pos]); Continue; } Q.push (Node (H.S, D[i)+H.path, POS)); F.insert (H.S); M[H.S]= D[i] +H.path; Swap (H.s[h.pos], H.s[pos]); } }}intMain () {pre (); while(~SCANF ("%s", input)) { stringv=""; V= v + input[0]; for(inti =1; I <=8; i++) {scanf ("%s", input); V= v + input[0]; } if(M[v] = ="") cout <<"unsolvable"<<Endl; Elsecout << M[v] <<Endl; } return 0;}
Realm Three, wide search + hash + Play table (HDU 263ms,poj c++797ms,poj g++579ms)
Use Cantor to expand the status of the Hash,hash value corresponding to 0--(9!) -1), so you can open an array of weight, the record of the path can record what the last step of reaching a state is and what the parent node is.
Start the BFS from the final state (0).
#include <cstdio>#include<cstring>#include<cmath>#include<vector>#include<map>#include<queue>#include<stack>#include<algorithm>using namespacestd;Chart[ +];intc[Ten];intdir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};Charpath[Ten]={'D','u','R','L'};Charop[5],input[ -];structnode{ints,p; Node () {} node (intSintP) {s=s,p=P;}};structpath{int from, dir;} pa[400000];BOOLf[400000];intGetnum () {intres=0; for(intI=0; t[i];i++) for(intj=i+1; t[j];j++) if(T[j]<t[i]) res=res+c[8-i]; returnRes;}voidGETSTR (intval) { inttmp[Ten],flag[Ten]; memset (Flag,0,sizeofflag); for(intI=0;i<9; i++) tmp[i]=val/c[8-i],val=val%c[8-i]; for(intI=0;i<9; i++) { intnum=0; for(intj=0;j<9; j + +) { if(flag[j]==0) num++; if(num==tmp[i]+1) {T[i]=j+'0'+1;if(t[j]=='9') t[j]='x'; FLAG[J]=1; Break; } } }}voidPre () {queue<Node>p; Q.push (Node (0,8)); f[0]=1; pa[0]. from=-1, pa[0].dir=-1; while(!Q.empty ()) {Node h=Q.front (); Q.pop (); inta=h.p/3, b=h.p%3; Getstr (H.S); for(intI=0;i<4; i++) { intx=a+dir[i][0],y=b+dir[i][1]; if(! (x>=0&&x<=2&&y>=0&&y<=2))Continue; intnewpos=3*x+y; Swap (T[NEWPOS],T[H.P]); intNews=Getnum (); if(F[news]) {swap (t[newpos],t[h.p]);Continue;} Pa[news]. from=H.S, Pa[news].dir=i, f[news]=1; Q.push (Node (News,newpos)); Swap (T[NEWPOS],T[H.P]); } }}intMain () {c[0]=1; for(intI=1; i<=8; i++) c[i]=c[i-1]*i; Pre (); while(~SCANF ("%s", op)) {t[0]=op[0]; for(intI=1; i<=8; i++) {scanf ("%s", op); t[i]=op[0];} intState=Getnum (); if(f[state]==0) printf ("unsolvable\n"); Else { while(1) { if(Pa[state]. from==-1) Break; printf ("%c", Path[pa[state].dir]); State=pa[state]. from; } printf ("\ n"); } } return 0;}
HDU 10,438 Digital (eight realms)