


There is a briefcase protected by a lock with 4 circular wheels. The password is a sequence of 4 digits.
1. Each wheel has 10 slots: β0β, β1β, β2β, β3β, β4β, β5β, β6β, β7β, β8β, β9β.
2. The wheels can rotate freely and wrap around, β9β can be left rotated to β8β and right rotated to β0β, similarly β0β can be right rotated to β1β and left rotated to β9β. Rotation of one wheel counts to one rotation.
3. Initially, the lock starts at β0000β, a string representing the state of four wheels.
You are given a list of dead ends (codes of 4 digits), meaning if at any point the lock displays any of these codes, the wheels of the lock will stop turning, and you will be unable to open it.
Given a target representing the code that will unlock the briefcase, return the minimum total number of turns required to open the lock, or -1 if it is not possible.
Input format:
The first line of input contains an integer βTβ denoting the number of test cases.
The first line of each test case contains an integer βNβ denoting the number of dead-end codes.
The second line of each test case contains the βNβ single-space separated dead-end codes.
The third line of each test case contains a string βtargetβ representing the unlocking code.
Output Format:
For each test case, print a single line containing a single integer denoting the minimum number of rotations will be printed.
The output of each test case is printed in a separate line.
Note:
You donβt have to print anything, it has already been taken care of. Just implement the function.
Constraints:
1 <= T <= 10
1 <= N <= 5 * 10 ^ 4
|TARGET| = 4
Where βTβ is the total number of test cases, βNβ represents the number of dead-end codes, and β|TARGET|β represents the length of the target.
Time limit: 1 sec.
1
5
0201 0101 0102 1212 2002
0202
6
A sequence of valid moves would be β0000β => β1000β => β1100β => β1200β => β1201β => β1202β => β0202β
Note that a sequence like β0000β => β0100β => β0200β => β0201β => β0202β would be invalid as these moves lead to a dead end β0201β after which the lock gets stuck and canβt be changed any further.
2
1
0000
8888
1
8888
0009
-1
1
Breadth First Search.
The idea is to use breadth first search. Find all possible codes that can be formed after one rotation in current code and for all those codes, find the next codes that can be formed after one rotation. These steps will lead to all possible codes that can be formed from the initial string. After, say βrβ number of rotations, the codes formed contain a code that is equal to the given target, you have reached to the target in minimum number of rotations that is βrβ.
Algorithm :
O(10 ^ 4).
There can be 10 * 10 * 10 * 10 = 10 ^ 4 possible number of codes that can be formed from the initial string β0000β. In BFS, every code is visited at most once in the worst case. Thus, the final time complexity will be O(10 ^ 4).
O(N), where N is the number of dead end codes.
Since we are storing all the visited codes in a map and a queue is used to store all the possible codes formed after every rotation consuming space almost equal to O(2 * (10 ^ 4)). All the dead-end codes in a map, which takes O(N). Thus, the final space complexity will be O(N + 2 * 10 ^ 4) = O(N).