Topic Link: http://acm.hdu.edu.cn/showproblem.php?pid=1195, two-way BFS or direct BFS can also be too.
In fact, this problem is only one-way BFS can be over, but in order to practice the algorithm, so still use a two-way bfs to write.
Algorithm:
First preprocessing, from 1111 to 9999 of all points of the composition (because it is 1~9, so that the number containing 0 elements), can be transformed into a number of numbers that are connected between two points. Then, the BFS is carried out from the initial state and the two points of the state, and if there is a trajectory coincident, return distance and.
Note here the two-way BFS to a layer of search, otherwise it will produce errors, as for the cause of the error is still thinking.
Bidirectional BFS Code:
#include <iostream>#include<cstdio>#include<vector>#include<queue>#include<cmath>#include<string>#include<string.h>#include<algorithm>using namespacestd;#defineLL __int64#defineEPS 1e-8#defineINF 1e8#defineLson L, M, RT << 1#defineRson m + 1, R, RT << 1 | 1Const intMOD =2333333; Const intMAXN =10000+5; Vector<int>E[MAXN];intVIS[MAXN], DIST[MAXN];voidSolveintx) { intnum[4], I, TMP, y; I=0; TMP=x; while(TMP) {num[i+ +] = tmp%Ten; TMP/=Ten; } for(i =0; I <4; i++) if(Num[i] = =0) return; for(i =0; I <4; i++) { if(I <3) {swap (num[i], num[i+1]); Y= num[3] * ++ num[2] * -+ num[1] *Ten+ num[0]; E[x].push_back (y); E[y].push_back (x); Swap (Num[i], num[i+1]); } tmp=Num[i]; if(Num[i] = =9) Num[i]=1; ElseNum[i]++; Y= num[3] * ++ num[2] * -+ num[1] *Ten+ num[0]; E[x].push_back (y); E[y].push_back (x); Num[i]=tmp; if(Num[i] = =1) Num[i]=9; ElseNum[i]--; Y= num[3] * ++ num[2] * -+ num[1] *Ten+ num[0]; E[x].push_back (y); E[y].push_back (x); Num[i]=tmp; }}intBfs_2 (intStart,intend) { if(Start = =end)return 0; memset (Vis,0,sizeof(VIS)); Queue<int> que[2]; Vis[start]=1; Vis[end]=2; que[0].push (start); que[1].push (end); Dist[start]= Dist[end] =0; while(!que[0].empty () &&!que[1].empty ()) {intK =0; if(que[0].size () < que[1].size ()) K++; intU =Que[k].front (); Que[k].pop (); for(inti =0; I < e[u].size (); i++) { intj =E[u][i]; if(!Vis[j]) {Vis[j]=Vis[u]; Que[k].push (j); DIST[J]= Dist[u] +1; } Else if(Vis[j] = =Vis[u]) { Continue; } Else { returnDIST[J] + Dist[u] +1; } } } return-1;}intMain () {intT, A, B; for(inti =1111; I <=9999; i++) solve (i); CIN>>T; while(t--) {scanf ("%d%d", &a, &b); printf ("%d\n", Bfs_2 (A, b)); } return 0;}
BFS Code:
#include <iostream>#include<cstdio>#include<vector>#include<queue>#include<cmath>#include<string>#include<string.h>#include<algorithm>using namespacestd;#defineLL __int64#defineEPS 1e-8#defineINF 1e8#defineLson L, M, RT << 1#defineRson m + 1, R, RT << 1 | 1Const intMOD =2333333; Const intMAXN =10000+5; Vector<int>E[MAXN];intVIS[MAXN], DIST[MAXN];voidSolveintx) { intnum[4], I, TMP, y; I=0; TMP=x; while(TMP) {num[i+ +] = tmp%Ten; TMP/=Ten; } for(i =0; I <4; i++) if(Num[i] = =0) return; for(i =0; I <4; i++) { if(I <3) {swap (num[i], num[i+1]); Y= num[3] * ++ num[2] * -+ num[1] *Ten+ num[0]; E[x].push_back (y); E[y].push_back (x); Swap (Num[i], num[i+1]); } tmp=Num[i]; if(Num[i] = =9) Num[i]=1; ElseNum[i]++; Y= num[3] * ++ num[2] * -+ num[1] *Ten+ num[0]; E[x].push_back (y); E[y].push_back (x); Num[i]=tmp; if(Num[i] = =1) Num[i]=9; ElseNum[i]--; Y= num[3] * ++ num[2] * -+ num[1] *Ten+ num[0]; E[x].push_back (y); E[y].push_back (x); Num[i]=tmp; }}intBFS (intAintb) { if(A = =b)return 0; memset (Vis,0,sizeof(VIS)); Queue<int>que; Que.push (a); Vis[a]=1; Dist[a]=0; while(!Que.empty ()) { intU =Que.front (); Que.pop (); for(inti =0; I < e[u].size (); i++) { intj =E[u][i]; if(J = =b)returnDist[u] +1; if(!Vis[j]) {Dist[j]= Dist[u] +1; VIS[J]=1; Que.push (j); } } }}intMain () {intT, A, B; for(inti =1111; I <=9999; i++) solve (i); CIN>>T; while(t--) {scanf ("%d%d", &a, &b); printf ("%d\n", BFS (A, b)); } return 0;}
HDU1195 Two-way BFS (or BFS)