Minimum Lock Turns
Given a starting lock combination (e.g. 0000) find the minimum number of turns required for each wheel to get to a target (e.g. 0202) in the presence of potentially blocked states.
Example 1:
Example 2:
Example 3:
Notes
Intuition
Each lock combination is a graph node with 8 neighbors (4 wheels × 2 directions). BFS from start to target finds the minimum number of turns, avoiding blocked combinations.
Implementation
Generate neighbors by turning each wheel up or down (using mod 10 for wraparound). Start BFS with (start_combo, 0) in the queue and a visited set. For each state, try all neighbors—skip if visited or blocked. If a neighbor is the target, return turns + 1. Otherwise, add to queue and visited.
Edge-cases
Check if start equals target (return 0) or if start is blocked (return -1) before starting BFS.
- Time: O(10⁴) — at most 10,000 possible states
- Space: O(10⁴) — visited set and queue