HDU 1195 Open the Lock

Source: Internet
Author: User

Topic Connection

http://acm.hdu.edu.cn/showproblem.php?pid=1195

Open the Lockdescription

Now the emergent task for are to open a password lock. The password is consisted of four digits. Each digit are numbered from 1 to 9.
Each time, you can add or minus 1 to any digit. When add 1 to ' 9 ', the digit would change to IS ' 1 ' and when minus 1 to ' 1 ', the digit would change to be ' 9 '. You can also exchange the digit with its neighbor. Each action would take one step.

Now your task was 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 is 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 a blank line after each test case.

Output

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

Sample Input

2
1234
2144

1111
9999

Sample Output

2
4

Simple wide-range question.

1#include <algorithm>2#include <iostream>3#include <cstdlib>4#include <cstring>5#include <cstdio>6#include <vector>7#include <queue>8#include <map>9 usingstd::cin;Ten usingstd::cout; One usingStd::endl; A usingStd::swap; - usingStd::sort; - usingStd::map; the usingstd::p air; - usingStd::queue; - usingstd::vector; - usingStd::multimap; + #definePB (E) push_back (e) - #defineSZ (c) (int) (c). Size () + #defineMP (A, b) Make_pair (A, B) A #defineAll (c) (c). Begin (), (c). End () at #defineITER (c) Decltype ((c). Begin ()) - #defineCLS (arr,val) memset (arr,val,sizeof (arr)) - #defineCpresent (c, E) (Find (All (c), (e))! = (c). End ()) - #defineRep (i, n) for (int i = 0; i < (int) (n); i++) - #defineTR (c, I) for (ITER (c) i = (c). Begin (); I! = (c). end (); ++i) - Const intN =10010; intypedef unsignedLong Longull; - BOOLVis[n]; to intstart, end; + structNode { -     intV, S; theNode (inti =0,intj =0): V (i), S (j) {} * }; $InlinevoidCalc_1 (int*arr,intv) {Panax Notoginseng     intI, T, j =0; -      Doarr[j++] = v%Ten; while(v/=Ten); the      for(i =0,--j; I < J; i++, j--) { +t =Arr[i]; AArr[i] =Arr[j]; theARR[J] =T; +     } - } $InlineintCalc_2 (int*arr) { $     intsum =0; -Rep (I,4) sum = sum *Ten+Arr[i]; -     returnsum; the } -InlinevoidCalc_3 (queue<node> &q,intKints) {Wuyi     if(!Vis[k]) { theQ.push (Node (k, S +1)); -VIS[K] =true; Wu     } - } About intBFs () { $     intV, K, tmp[Ten]; -CLS (Vis,false); -Queue<node>Q; -Q.push (Node (Start,0)); AVis[start] =true; +      while(!Q.empty ()) { theNode T =Q.front (); Q.pop (); -         if(T.V = = end)returnT.s; $ calc_1 (TMP, T.V); theRep (I,4) { thev =Tmp[i]; the             if(v +1==Ten) Tmp[i] =1; the             ElseTmp[i] = v +1; -K =calc_2 (TMP); in Calc_3 (q, K, t.s); theTmp[i] =v; the             if(V-1==0) Tmp[i] =9; About             ElseTmp[i] = v-1; theK =calc_2 (TMP); the Calc_3 (q, K, t.s); theTmp[i] =v; +         } -Rep (I,3) { the calc_1 (TMP, T.V);BayiSwap (Tmp[i], tmp[i +1]); theK =calc_2 (TMP); the Calc_3 (q, K, t.s); -         } -     } the     return 0; the } the intMain () { the #ifdef LOCAL -Freopen ("In.txt","R", stdin); theFreopen ("OUT.txt","w+", stdout); the #endif the     intT;94scanf"%d", &t); the      while(t--) { thescanf"%d%d", &start, &end); theprintf"%d\n", BFS ());98     } About     return 0; -}
View Code

HDU 1195 Open the Lock

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.