-> Question, click here <-
It takes at least a few steps to change from the first string to the second string.
Changes in each step include:
1: Each number is + 1, 9 + 1 = 1;
2: Each number-1, 1-1 = 9;
3: A number is exchanged with its adjacent number. The first and last digits are not adjacent.
Question Analysis: BFS ~, This one-way BFS can also be used. However, if you want to practice two-way BFs, it is the first time to write two-way BFs. So first, find a simple cut. The queue decides to write it by itself, saving time, and saving space with cyclic queues. However, it seems that it is not very fast to run out, and it cannot be 0 ms. Well, I'm down again...
Unidirectional BFS:
# Include <iostream> # include <queue> # include <cstring> using namespace STD; char S1 [5], S2 [5]; bool mark [10] [10] [10] [10]; struct node {char s [5]; int count;} t, now, last; void BFS () {queue <node> q; memset (mark, 0, sizeof (Mark); strcpy (T. s, S1); T. count = 0; Mark [T. s [0]-'0'] [T. s [1]-'0'] [T. s [2]-'0'] [T. s [3]-'0'] = 1; q. push (t); While (! Q. empty () {now = Q. front (); q. pop (); If (strcmp (S2, now. s) = 0) {printf ("% d \ n", now. count); return;} Char C; For (INT I = 0; I <4; I ++) {if (I <3) // replace {strcpy (T. s, now. s); C = T. s [I]; T. s [I] = T. s [I + 1]; T. s [I + 1] = C; If (! Mark [T. s [0]-'0'] [T. s [1]-'0'] [T. s [2]-'0'] [T. s [3]-'0']) {T. count = now. count + 1; Mark [T. s [0]-'0'] [T. s [1]-'0'] [T. s [2]-'0'] [T. s [3]-'0'] = 1; if (strcmp (S2, T. s) = 0) {printf ("% d \ n", T. count); return;} Q. push (t) ;}} strcpy (T. s, now. s); T. s [I] = now. s [I] + 1; // Add if (T. s [I]-'0'> 9) T. s [I] = '1'; If (! Mark [T. s [0]-'0'] [T. s [1]-'0'] [T. s [2]-'0'] [T. s [3]-'0']) {T. count = now. count + 1; Mark [T. s [0]-'0'] [T. s [1]-'0'] [T. s [2]-'0'] [T. s [3]-'0'] = 1; if (strcmp (S2, T. s) = 0) {printf ("% d \ n", T. count); return;} Q. push (t);} strcpy (T. s, now. s); T. s [I] = now. s [I]-1; // subtract if (T. s [I]-'0' = 0) T. s [I] = '9'; If (! Mark [T. s [0]-'0'] [T. s [1]-'0'] [T. s [2]-'0'] [T. s [3]-'0']) {T. count = now. count + 1; Mark [T. s [0]-'0'] [T. s [1]-'0'] [T. s [2]-'0'] [T. s [3]-'0'] = 1; if (strcmp (S2, T. s) = 0) {printf ("% d \ n", T. count); return;} Q. push (t) ;}}} int main () {int t; scanf ("% d", & T); While (t --) {scanf ("% S % s", S1, S2); BFS () ;}return 0 ;}// 31 Ms 344 K
Bidirectional BFS:
# Include <iostream> # include <cstdio> # include <cstring> # include <cstdlib> # include <cctype> using namespace STD; int flag [2] [10] [10] [10] [10]; char S1 [10], S2 [10]; char ss [10], now [10]; struct que {char s [100005] [10]; int head, tail, size; void Init () {head = tail = 0; size = 100005;} bool empty () {return head = tail;} Char * Top () {return s [head];} void POP () {head ++; head % = size ;} void push (char a []) {Strcpy (s [tail], a); tail ++; tail % = size ;}} Q [2]; int BFS () {int I, J; Q [0]. init (); Q [1]. init (); int step = 0; memset (flag,-1, sizeof (FLAG); If (strcmp (S1, S2) = 0) return 0; Q [0]. push (S1); // forward search Q [1]. push (S2 ); // reverse search flag [0] [S1 [0]-'0'] [S1 [1]-'0'] [S1 [2]-'0'] [S1 [3]-'0'] = 0; flag [1] [S2 [0]-'0'] [S2 [1]-'0'] [S2 [2]-'0'] [S2 [3]- '0'] = 0; while (! Q [0]. Empty ()&&! Q [1]. empty () {for (I = 0; I <= 1; I ++) {strcpy (now, Q [I]. top (); Q [I]. pop (); step = Flag [I] [now [0]-'0'] [now [1]-'0'] [now [2]-'0'] [now [3 ]-'0']; for (j = 0; j <4; j ++) {If (j <3) // exchange with the right {strcpy (SS, now ); char c = ss [J]; SS [J] = ss [J + 1]; SS [J + 1] = C; if (flag [I] [ss [0]-'0'] [ss [1]-'0'] [ss [2]-'0'] [ss [3 ]-'0'] =-1) {flag [I] [ss [0]-'0'] [ss [1]-'0'] [ss [2]-'0'] [ss [3] -'0'] = 1 + step; If (flag [1-I] [ss [0]-'0'] [ss [1]-'0'] [ss [2]-'0'] [ss [3]-'0']! =-1) {return 1 + step + flag [1-I] [ss [0]-'0'] [ss [1]-'0'] [ss [2]-'0 '] [ss [3]-'0'];} Q [I]. push (SS) ;}} strcpy (SS, now); If (ss [J] = '9') ss [J] = '1 '; else ss [J] ++; if (flag [I] [ss [0]-'0'] [ss [1]-'0'] [ss [2]-'0'] [ss [3 ]-'0'] =-1) {flag [I] [ss [0]-'0'] [ss [1]-'0'] [ss [2]-'0'] [ss [3] -'0'] = 1 + step; if (flag [1-I] [ss [0]-'0'] [ss [1]-'0'] [ss [2]-'0'] [ss [3]-'0']! =-1) {return 1 + step + flag [1-I] [ss [0]-'0'] [ss [1]-'0'] [ss [2]-'0 '] [ss [3]-'0'];} Q [I]. push (SS) ;}strcpy (SS, now); If (ss [J] = '1') ss [J] = '9 '; else ss [J] --; if (flag [I] [ss [0]-'0'] [ss [1]-'0'] [ss [2]-'0'] [ss [3 ]-'0'] =-1) {flag [I] [ss [0]-'0'] [ss [1]-'0'] [ss [2]-'0'] [ss [3] -'0'] = 1 + step; if (flag [1-I] [ss [0]-'0'] [ss [1]-'0'] [ss [2]-'0'] [ss [3]-'0']! =-1) {return 1 + step + flag [1-I] [ss [0]-'0'] [ss [1]-'0'] [ss [2]-'0 '] [ss [3]-'0'];} Q [I]. push (SS) ;}} if (! Q [I]. Empty () // you need to search the same layer... {Strcpy (now, Q [I]. top ()); if (flag [I] [now [0]-'0'] [now [1]-'0'] [now [2]-'0'] [now [3 ]-'0'] = step) I -- ;}}} void input () {char C; while (isspace (C = getchar (); S1 [0] = C; s1 [1] = getchar (); S1 [2] = getchar (); S1 [3] = getchar (); While (isspace (C = getchar ())); s2 [0] = C; S2 [1] = getchar (); S2 [2] = getchar (); S2 [3] = getchar ();} int main () {int t; scanf ("% d", & T); While (t --) {// scanf ("% S % s ", S1, S2); input (); // still very slow... Printf ("% d \ n", BFs ();} return 0;} // 15ms440k