An interactive visualization of the BFS approach to Open the Lock. Watch BFS explore the combination space, turning dials to find the shortest path from "0000" to the target while avoiding deadends.
BFS LeetCode 752 โ Medium'0', '1', '2', ..., '9'. The wheels can rotate freely and wrap around:
for example, we can turn '9' to be '0', or '0' to be '9'.
Each move consists of turning one wheel one slot."0000", a string representing the state of the 4 wheels.deadends dead ends, meaning if the lock displays any of these codes,
the wheels of the lock will stop turning and you cannot open it.target representing the value of the wheels when the lock is unlocked,
return the minimum total number of turns required to open the lock,
or -1 if it is impossible.
fun openLock(deadends: Array<String>, target: String): Int { val dead = deadends.toHashSet() if ("0000" in dead) return -1 val queue: ArrayDeque<Pair<String, Int>> = ArrayDeque() queue.add("0000" to 0) val visited = mutableSetOf("0000") while (queue.isNotEmpty()) { val (combo, turns) = queue.removeFirst() if (combo == target) return turns for (i in 0 until 4) { val d = combo[i] - '0' for (nd in intArrayOf((d+1)%10, (d+9)%10)) { val new = combo.substring(0,i) + nd + combo.substring(i+1) if (new !in visited && new !in dead) { visited.add(new) queue.add(new to turns + 1) } } } } return -1 }
Think of each 4-digit combination as a node in a graph, and each single-dial turn as an edge connecting two nodes. There are 10,000 possible combinations (0000-9999), and each node has exactly 8 neighbors (4 dials x 2 directions). Finding the minimum turns is equivalent to finding the shortest path in an unweighted graph -- the classic BFS problem.
"0000" is a deadend, return -1 immediately."0000" in the queue at distance 0. Mark it visited.
Where N = 4 (number of dials) and D is the number of deadends.
In the worst case, BFS visits all 10,000 combinations. Each combination generates 8 neighbors,
and creating each neighbor string takes O(N) time. The visited set and queue can hold up to 10^N entries.