Open the Lock
Recognize the pattern
Brute force idea
A straightforward first read of Open the Lock is this: DFS trying all combinations — doesn't find shortest path efficiently. That instinct is useful because it follows the prompt literally, but it usually keeps revisiting work the problem is begging you to organize.
Better approach
A calmer way to see Open the Lock is this: BFS on the state space: each state is a 4-digit string. From each state, generate 8 neighbors (each digit ±1). Skip deadends. The first time you reach the target is the shortest path. The goal is not to be clever for its own sake, but to remember the one relationship that keeps the solution grounded as you move forward.
Key invariant
The truth you want to protect throughout Open the Lock is this: BFS guarantees shortest path. Each state has exactly 8 neighbors (4 digits × 2 directions). The state space is bounded at 10,000 possible combinations. If that remains true after every update, the rest of the reasoning has a stable place to stand.
Watch out for
One easy way to drift off course in Open the Lock is this: Not pre-loading deadends into the visited set — deadends should be marked as visited before BFS starts so they're never explored. The fix is usually to return to the meaning of each move, not just the steps themselves.