The lock's 4 wheels represent an unmaterialized state graph with $10^4 = 10,000$ vertices. Pre-seed deadends into the visited set and launch a level-by-level BFS across 8 rotational neighbors per node.
visited set before BFS starts, making deadend avoidance $O(1)$ and eliminating special case checks.
"The graph is not given as an adjacency list. The vertices are strings from '0000' to '9999' ($10^4 = 10,000$ possible nodes). Each state has exactly 8 neighbors formed by turning each of the 4 wheels $\pm 1$ modulo 10."
"Rather than repeatedly querying a deadends set inside the loop, we initialize our visited set with all deadends upfront. If '0000' is a deadend, it is caught instantly at step 0."
"Because each wheel turn costs 1 unit, BFS guarantees finding the minimum number of turns to reach the target on the first encounter."
"For interview depth, mention bidirectional BFS: expanding simultaneously from '0000' and 'target' reduces search complexity from $b^d$ to $2 b^{d/2}$ where $b=8$."