You are given a lock with '0' through '9'.
The wheels can rotate freely and wrap around cyclically; turning '9' forward leads to '0', and turning '0’ backward leads to '9'.
Each move consists of rotating one wheel by one position (either forward or backward).
The lock starts from the initial state "0000".
You are also given a list of deadends, where each string represents a lock position that is blocked; if the lock reaches any of these positions, it becomes stuck and can no longer be turned.
Your goal is to reach a given target combination by performing the minimum number of moves, starting from "0000" and avoiding all dead ends.
If it is impossible to reach the target, return -1.
Constraints:
deadends.length
deadends[i].length
target.length
target will not be in the list deadends.
target and deadends[i] consists of digits only.
The solution uses a Breadth-First Search (BFS) approach to find the minimum number of moves needed to reach the target lock combination from "0000", while avoiding dead ends. The key idea is to treat each -1.
Now, let’s walk through the steps of the solution:
Create two character maps, nextSlot to map the current slot digit with its next slot digitprevSlot to map the current slot digit to its previous slot digit
Create a hash set, visited, initially containing all the combinations in deadends array.
Create a queue, pending to traverse all combinations in BFS.
Create an integer variable, turns, initially 0, to denote the number of wheel turns (BFS levels) made so far.
If visited contains the starting combination "0000" then we can never reach the target combination and will return -1 .
Otherwise, insert the starting combination "0000", into pending and mark it as visited by inserting it into visited.
While there are elements in pending, iterate on all current level combinations, which is equivalent to the current queue size (number of states at this distance) times:
Pop a combination from the queue and store it in currentCombination.
If currentCombination == target, return turns.
Otherwise, iterate on all four wheels and for each wheel index,
Turn the wheel forward using nextSlot to form a newCombination.
If newCombination is not in visited, add it to the pending and mark it visited.
Turn the same wheel backward using prevSlot to form another newCombination.
If this is also unseen, add it to the pending and mark it visited.
After iterating on all current level combinations, increment turns.
If we never reach the target combination, return -1.
Let’s look at the following illustration to get a better understanding of the solution:
Let’s look at the code for the solution we just discussed.
Building nextSlot and prevSlot maps, each mapping every digit to one neighbor. Each map has
Inserting all deadend combinations of length
BFS over the state space:
Total possible combinations of the lock:
You are given a lock with '0' through '9'.
The wheels can rotate freely and wrap around cyclically; turning '9' forward leads to '0', and turning '0’ backward leads to '9'.
Each move consists of rotating one wheel by one position (either forward or backward).
The lock starts from the initial state "0000".
You are also given a list of deadends, where each string represents a lock position that is blocked; if the lock reaches any of these positions, it becomes stuck and can no longer be turned.
Your goal is to reach a given target combination by performing the minimum number of moves, starting from "0000" and avoiding all dead ends.
If it is impossible to reach the target, return -1.
Constraints:
deadends.length
deadends[i].length
target.length
target will not be in the list deadends.
target and deadends[i] consists of digits only.
The solution uses a Breadth-First Search (BFS) approach to find the minimum number of moves needed to reach the target lock combination from "0000", while avoiding dead ends. The key idea is to treat each -1.
Now, let’s walk through the steps of the solution:
Create two character maps, nextSlot to map the current slot digit with its next slot digitprevSlot to map the current slot digit to its previous slot digit
Create a hash set, visited, initially containing all the combinations in deadends array.
Create a queue, pending to traverse all combinations in BFS.
Create an integer variable, turns, initially 0, to denote the number of wheel turns (BFS levels) made so far.
If visited contains the starting combination "0000" then we can never reach the target combination and will return -1 .
Otherwise, insert the starting combination "0000", into pending and mark it as visited by inserting it into visited.
While there are elements in pending, iterate on all current level combinations, which is equivalent to the current queue size (number of states at this distance) times:
Pop a combination from the queue and store it in currentCombination.
If currentCombination == target, return turns.
Otherwise, iterate on all four wheels and for each wheel index,
Turn the wheel forward using nextSlot to form a newCombination.
If newCombination is not in visited, add it to the pending and mark it visited.
Turn the same wheel backward using prevSlot to form another newCombination.
If this is also unseen, add it to the pending and mark it visited.
After iterating on all current level combinations, increment turns.
If we never reach the target combination, return -1.
Let’s look at the following illustration to get a better understanding of the solution:
Let’s look at the code for the solution we just discussed.
Building nextSlot and prevSlot maps, each mapping every digit to one neighbor. Each map has
Inserting all deadend combinations of length
BFS over the state space:
Total possible combinations of the lock: