HDU1195Open the Lock (BFS)

Source: Internet
Author: User

HDU1195Open the Lock (BFS)

Open the Lock

Description

Now 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.

Input

The 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.

Output

For each test case, print the minimal steps in one line.

Sample Input

 21234214411119999 

Sample Output

 24 
# Include
 
  
# Include
  
   
# Include
   
    
Using namespace std; struct node {int a; int t ;}; int s, e; int toDigit (int a []) {int ans = 0; for (int I = 0; I <4; I ++) ans = ans * 10 + a [I]; return ans;} void toSprit (int a [], int ans) {int I = 4; while (I> 0) {a [-- I] = ans % 10; ans/= 10 ;}} int bfs () {queue
    
     
Q; node p, tp; int vist [10000] = {0}, a [6]; if (s = e) return 0; p. a = s; p. t = 0; q. push (p); vist [s] = 1; while (! Q. empty () {p = q. front (); q. pop (); toSprit (a, p. a); tp. t = p. t + 1; for (int I = 0; I <4; I ++) {if (a [I] = 9) a [I] = 1; // + 1 else a [I] + = 1; tp. a = toDigit (a); if (tp. a = e) return tp. t; if (vist [tp. a] = 0) q. push (tp), vist [tp. a] = 1; if (a [I] = 1) a [I] = 9; else a [I] --; if (a [I] = 1) //-1 a [I] = 9; else a [I]-= 1; tp. a = toDigit (a); if (tp. a = e) return tp. t; if (vist [tp. a] = 0) q. push (tp), vist [tp. a] = 1; if (a [I] = 9) a [I] = 1; else a [I] ++; if (I <3) // exchange with the previous {int tem = a [I]; a [I] = a [I + 1]; a [I + 1] = tem; tp. a = toDigit (a); if (tp. a = e) return tp. t; if (vist [tp. a] = 0) q. push (tp), vist [tp. a] = 1; tem = a [I]; a [I] = a [I + 1]; a [I + 1] = tem ;}}} return-1 ;}int main () {int t; scanf ("% d", & t); while (t --) {scanf ("% d ", & s, & e); int time = bfs (); printf ("% d \ n", time );}}
    
   
  
 


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.