Problem
Ask
Submissions

Problem: Open the Lock

Medium
30 min
Explore the Open the Lock problem by applying Tree Breadth-First Search techniques to find the minimum moves from '0000' to a target combination while avoiding deadend states. Understand how to systematically traverse and solve problems involving cyclic wheel rotations and blocked positions.

Statement

You are given a lock with 44 circular wheels, each containing digits '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:

  • 11 \leq deadends.length 500\leq 500

  • deadends[i].length ==4== 4

  • target.length ==4== 4

  • target will not be in the list, deadends.

  • Both target and deadends[i] consist of digits only.

Problem
Ask
Submissions

Problem: Open the Lock

Medium
30 min
Explore the Open the Lock problem by applying Tree Breadth-First Search techniques to find the minimum moves from '0000' to a target combination while avoiding deadend states. Understand how to systematically traverse and solve problems involving cyclic wheel rotations and blocked positions.

Statement

You are given a lock with 44 circular wheels, each containing digits '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:

  • 11 \leq deadends.length 500\leq 500

  • deadends[i].length ==4== 4

  • target.length ==4== 4

  • target will not be in the list, deadends.

  • Both target and deadends[i] consist of digits only.