Skip to content

752. Open the Lock #172

Jul 30, 2024 · 1 comments · 2 replies
Discussion options

You must be logged in to vote

We can use Breadth-First Search (BFS) to find the shortest path from the initial state '0000' to the target while avoiding the deadends.

Key Concepts:

  1. Graph Representation:

    • Each lock state (e.g., '0000', '1234') is a node.
    • Each move consists of changing one wheel either forward (increment by 1) or backward (decrement by 1). This gives a total of 8 possible moves from any given state (2 for each of the 4 wheels).
  2. BFS Approach:

    • We start at the initial state '0000' and try all possible moves.
    • If a state matches a deadend or has been visited before, we skip it.
    • Once we reach the target, we return the number of moves (the depth of BFS).
    • If we explore all possibilities and don't reach th…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@mah-shamim
Comment options

mah-shamim Sep 15, 2024
Maintainer Author

@basharul-siddike
Comment options

Answer selected by mah-shamim
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested medium Difficulty
2 participants