bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 773. Sliding Puzzle

773. Sliding Puzzle

Leetcode Solutions

Breadth-First Search (BFS) Approach

  1. Convert the 2D board into a 1D tuple to make it hashable.
  2. Initialize a queue with the starting board state and a depth of 0.
  3. Initialize a set to keep track of seen (visited) board states.
  4. While the queue is not empty, dequeue the front node.
  5. If the node is the target state, return the depth (number of moves).
  6. Otherwise, for each neighbor (reachable state by making one move), if it has not been seen, add it to the seen set and enqueue it with an incremented depth.
  7. If the queue is exhausted without finding the target state, return -1, indicating the solution is not possible.
UML Thumbnail

A* Search Approach

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...