Sliding Puzzle - Complete Solution Guide
Sliding Puzzle is LeetCode problem 773, a Hard level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
On an 2 x 3 board, there are five tiles labeled from 1 to 5 , and an empty square represented by 0 . A move consists of choosing 0 and a 4-directionally adjacent number and swapping it. The state of the board is solved if and only if the board is [[1,2,3],[4,5,0]] . Given the puzzle board board , return the least number of moves required so that the state of the board is solved . If it is impossible for the state of the board to be solved, return -1 . Example 1: Input: board = [[1,2,3],[4,0,5]]
Detailed Explanation
The problem presents a sliding puzzle on a 2x3 board with tiles numbered 1 to 5 and an empty space represented by 0. The goal is to find the minimum number of moves (swapping the 0 with an adjacent tile) to reach the solved state [[1, 2, 3], [4, 5, 0]]. If the puzzle cannot be solved, the function should return -1. The input is a 2x3 integer array representing the initial state of the board, and the output is an integer representing the minimum number of moves or -1 if unsolvable.
Solution Approach
The solution employs Breadth-First Search (BFS) to explore the possible states of the puzzle. The initial state is the root node. Each neighbor of a node represents a valid move (swapping the 0 with an adjacent number). BFS guarantees finding the shortest path to the target state. A set is used to keep track of visited states to prevent cycles and redundant computations. The `neighbors` data structure speeds up the determination of valid moves from each position.
Step-by-Step Algorithm
- Step 1: Initialize the target state as a tuple (1, 2, 3, 4, 5, 0).
- Step 2: Convert the input 2D board into a 1D tuple representing the start state.
- Step 3: Create a `neighbors` dictionary (or array) to store the valid moves for each position of the '0'.
- Step 4: Initialize a queue with the starting state and the number of moves (initially 0).
- Step 5: Initialize a set to keep track of visited states to avoid revisiting them and entering infinite loops.
- Step 6: While the queue is not empty:
- Step 7: Dequeue a state and its corresponding number of moves.
- Step 8: Find the index of '0' in the current state.
- Step 9: Iterate through the possible neighbor indices for the '0'.
- Step 10: Generate the next state by swapping '0' with its neighbor.
- Step 11: If the next state is the target state, return the current number of moves + 1.
- Step 12: If the next state has not been visited, add it to the visited set and enqueue it with the number of moves + 1.
- Step 13: If the queue becomes empty and the target state has not been found, return -1 (puzzle is unsolvable).
Key Insights
- Insight 1: The problem can be modeled as a graph where each state of the board is a node, and edges represent possible moves. Breadth-First Search (BFS) is suitable for finding the shortest path in a graph.
- Insight 2: States can be represented as strings or tuples to easily track visited states and avoid cycles.
- Insight 3: Using a `neighbors` array/map to store the possible moves from each position of the zero avoids redundant calculations.
- Insight 4: Deterministic problems like this one might not always be solvable, so a check for unsolvability should be implemented if applicable (though it's not strictly necessary here, BFS will naturally terminate with -1).
- Insight 5: Transforming the 2D array into a 1D array (or tuple/string) simplifies the swapping logic and state comparison.
Complexity Analysis
Time Complexity: O(6!)
Space Complexity: O(6!)
Topics
This problem involves: Array, Dynamic Programming, Backtracking, Breadth-First Search, Memoization, Matrix.
Companies
Asked at: Airbnb, Nvidia.