The 8 Puzzle is the smaller variant of the 15Puzzle (or the Sliding puzzle).
You have a 3x3
grid which is filled with numbers from 0-8 (0 denotes the blank tile) arranged in a random order.
Your task is to input a 3x3 grid and show the shortest solution (minimum moves) to get to the goal state. Display each boardstate including the first state in the output.
There may be multiple optimal solutions, you just need to print one.
Input: (small example)
1 2 0
4 5 3
7 8 6
Output:
2 <- denotes minimum number of moves required
1 2 0
4 5 3
7 8 6
1 2 3
4 5 0
7 8 6
1 2 3
4 5 6
7 8 0 <- goal state
If the puzzle can't be solved, print just -1
(denoting unsolvable)
Edit: Time limit : < 30seconds.
grid which is filled with numbers from 0-9
begrid which is filled with numbers from 0-8
? \$\endgroup\$