VOOZH about

URL: https://www.geeksforgeeks.org/dsa/unlock-the-lock/

⇱ Unlock the Lock - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Unlock the Lock

Last Updated : 13 Dec, 2025

We have a lock with four wheels, each showing a digit from 0 to 9. The lock starts at "0000". In one move, we can rotate any single wheel one step forward or backward, with wrap-around between 0 and 9.

Some combinations are blocked and cannot be used. If the lock reaches any blocked combination, it gets stuck and can no longer be moved.

Given a target combination and a list of blocked combinations, find the minimum number of moves needed to reach the target from "0000" without ever entering a blocked state. If this is not possible, return -1.

Examples:

Input: barriers[] = ["0201", "0101", "0102", "1212", "2002"], target = "0202"
Output: 6
Explanation:
A sequence of valid moves would be "0000" -> "1000"
-> "1100" -> "1200" -> "1201" -> "1202" -> "0202".
Note that a sequence like "0000" -> "0001" -> "0002"
-> "0102" -> "0202" would be invalid, because the wheels of the lock stuck after the display becomes the barrier "0102".

Input: barrier[] = ["0000"], target = "8888"
Output: -1
Explanation: The lock starts at "0000", which itself is a barrier. Since the lock becomes unusable immediately, no moves can be made. Therefore, it is impossible to reach the target combination, and the answer is -1.

[Naive Approach] Recursive Backtracking (DFS) – O(8^n) time and O(n) space

The simple idea is to try to explore all possible paths using DFS. From the current state, we generate all 8 neighbors and recursively move forward, tracking the minimum steps when we reach the target. We keep a visited set for the current path to avoid simple cycles, but overall the number of paths can still explode exponentially.

[Better Approach] Single - Ended BFS with Hashing

Here we treat each 4-digit string as a node and edges as single moves. We perform a normal BFS starting from "0000", exploring states level-by-level. The first time we reach the target, the current BFS depth gives the minimum number of moves. We use hash sets for barriers and visited states.


Output
6

Time Complexity: O(10n × n), where n = 4
Auxiliary Space: O(b + 10n), where b = barriers size, n = 4

[Expected Approach 1] Optimized BFS Using Indexing

We can further optimize BFS by avoiding strings in hash sets for visited and barriers. Each 4-digit combination can be mapped to an integer in [0, 9999] (like "abcd" -> 1000*a + 100*b + 10*c + d). We then store blocked and vis in simple vector<bool> arrays which are faster and more memory-efficient than hash sets.


Output
6

Time Complexity: O(b + 10n × n), where b = barriers size, n = 4
Auxiliary Space: O(10n),, where n = 4

[Expected Approach 2] Bidirectional BFS

Bidirectional BFS runs two BFS searches: one from "0000" and one from target. Each step, we expand the smaller frontier. When a state generated from one side already exists in the opposite side’s visited set, we know the paths meet there and we have found the minimum number of moves. This often explores many fewer states than normal BFS in practice.


Output
6

Time Complexity: O(b + 10n × n), where b = barriers size, n = 4
Auxiliary Space: O(b + 10n), where n = 4

Comment
Article Tags:
Article Tags: