Open the lock
Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 2919 accepted submission (s): 1276
Problem descriptionnow an emergent task for you is to open a password lock. The password is consisted of four digits. Each digit is numbered from 1 to 9.
Each time, you can add or minus 1 to any digit. when Add 1 to '9', the digit will change to be '1' and when minus 1 to '1', the digit will change to be '9 '. you can also exchange the digit with its neighbor. each action will take one step.
Now your task is to use minimal steps to open the lock.
Note: The leftmost digit is not the neighbor of the rightmost digit.
Inputthe input file begins with an integer T, indicating the number of test cases.
Each test case begins with a four digit N, indicating the initial state of the password lock. then followed a line with anotther four dight M, indicating the password which can open the lock. there is one blank line after each test case.
Outputfor each test case, print the minimal steps in one line.
Sample Input
21234214411119999
Sample output
24
Authorye, Kai
Sourcezhejiang University local contest 2005
Recommendignatius. L
Solution: the question entitled "breadth-first search" may be unexpected. But it is indeed seeking the shortest path, but it is different from the general maze search. The search tree here is based on the meaning of the question, not the top, bottom, but add, subtract, change left, change right. Always remember the features of the breadth-first search, the shortest path, and the traversal tag. Be sure to mark the searched status. Otherwise, the waiting result will be: time-out. I hope you will learn from me ......
# Include <cstdio> # include <cstring> # include <algorithm> # include <queue> using namespace STD; int A [4]; int B [4]; int f [10] [10] [10] [10]; struct node {int X [4]; int step ;}; int BFS () {int I, J; node S, E; queue <node> q; memset (F, 0, sizeof (f); S. X [0] = A [0]; S. X [1] = A [1]; S. X [2] = A [2]; S. X [3] = A [3]; S. step = 0; q. push (s); // int n = 20; F [A [0] [A [1] [A [2] [A [3] = 1; while (! Q. empty () {S = Q. front (); q. pop (); If (S. X [0] = B [0] & S. X [1] = B [1] & S. X [2] = B [2] & S. X [3] = B [3]) return S. step; // printf ("% d \ n", S. X [0], S. X [1], S. X [2], S. X [3]); for (I = 0; I <4; I ++) {// if (I! = 0) {e. X [0] = S. X [0]; E. X [1] = S. X [1]; E. X [2] = S. X [2]; E. X [3] = S. X [3]; j = E. X [I-1]; E. [I-1] = E. X [I]; E. X [I] = J; E. step = S. step + 1; if (! F [E. X [0] [E. X [1] [E. X [2] [E. X [3]) {q. push (E); F [E. X [0] [E. X [1] [E. X [2] [E. X [3] = 1 ;}} if (E. X [0] = B [0] & E. X [1] = B [1] & E. X [2] = B [2] & E. X [3] = B [3]) Return e. step; // if (I! = 3) {e. X [0] = S. X [0]; E. X [1] = S. X [1]; E. X [2] = S. X [2]; E. X [3] = S. X [3]; j = E. X [I]; E. X [I] = E. X [I + 1]; E. X [I + 1] = J; E. step = S. step + 1; if (! F [E. X [0] [E. X [1] [E. X [2] [E. X [3]) {q. push (E); F [E. X [0] [E. X [1] [E. X [2] [E. X [3] = 1 ;}} if (E. X [0] = B [0] & E. X [1] = B [1] & E. X [2] = B [2] & E. X [3] = B [3]) Return e. step; // X [I] + 1; // printf "" (); E. X [0] = S. X [0]; E. X [1] = S. X [1]; E. X [2] = S. X [2]; E. X [3] = S. X [3]; E. X [I] + = 1; if (E. X [I] = 10) E. X [I] = 1; E. step = S. step + 1; if (! F [E. X [0] [E. X [1] [E. X [2] [E. X [3]) {q. push (E); F [E. X [0] [E. X [1] [E. X [2] [E. X [3] = 1;} If (E. X [0] = B [0] & E. X [1] = B [1] & E. X [2] = B [2] & E. X [3] = B [3]) Return e. step; // X [I]-1 E. X [0] = S. X [0]; E. X [1] = S. X [1]; E. X [2] = S. X [2]; E. X [3] = S. X [3]; E. X [I]-= 1; if (E. X [I] = 0) E. X [I] = 9; E. step = S. step + 1; if (! F [E. X [0] [E. X [1] [E. X [2] [E. X [3]) {q. push (E); F [E. X [0] [E. X [1] [E. X [2] [E. X [3] = 1;} If (E. X [0] = B [0] & E. X [1] = B [1] & E. X [2] = B [2] & E. X [3] = B [3]) Return e. step ;}}int main () {int t; scanf ("% d", & T); While (t --) {scanf ("% 1D % 1D % 1D % 1D", & A [0], & A [1], & A [2], & A [3]); scanf ("% 1D % 1D % 1D % 1D", & B [0], & B [1], & B [2], & B [3]); // printf ("% d \ n", a [0], a [1], a [2], a [3]); // printf ("% d \ n", B [0], B [1], B [2], B [3]); printf ("% d \ n", BFs ();} return 0 ;}