Given a 4×4 board with 15 tiles (every tile has one number from 1 to 15) and one empty space. The objective is to place the numbers on tiles in order using the empty space. We can slide four adjacent (left, right, above and below) tiles into the empty space. For example,
Here X marks the spot to where the elements can be shifted and the final configuration always remains the same the puzzle is solvable.
In general, for a given grid of width N, we can find out check if a N*N – 1 puzzle is solvable or not by following below simple rules :
- If N is odd, then puzzle instance is solvable if number of inversions is even in the input state.
- If N is even, puzzle instance is solvable if
- the blank is on an even row counting from the bottom (second-last, fourth-last, etc.) and number of inversions is odd.
- the blank is on an odd row counting from the bottom (last, third-last, fifth-last, etc.) and number of inversions is even.
- For all other cases, the puzzle instance is not solvable.
What is an inversion here?
If we assume the tiles written out in a single row (1D Array) instead of being spread in N-rows (2D Array), a pair of tiles (a, b) form an inversion if a appears before b but a > b.
For above example, consider the tiles written out in a row, like this:
2 1 3 4 5 6 7 8 9 10 11 12 13 14 15 X
The above grid forms only 1 inversion i.e. (2, 1).
Below is a simple C++ program to check whether a given instance of 15 puzzle is solvable or not. The program is generic and can be extended to any grid width.
Time Complexity : O(n2)
Space Complexity: O(n)
How does this works?
Fact 1: For a grid of odd width, all legal moves preserve the polarity (even or odd) of the number of inversions.
Proof of Fact 1
- Moving a tile along the row (left or right) doesn’t change the number of inversions, and therefore doesn’t change its polarity.
- Moving a tile along the column (up or down) can change the number of inversions. The tile moves past an even number of other tiles (N – 1). So move either increases/decreases inversion count by 2, or keeps the inversion count same.
Fact 2: For a grid of even width, the following is invariant: (#inversions even) == (blank on odd row from bottom).
Example: Consider the move above. The number of inversions on the left is 49, and the blank is on an even row from the bottom. So the value of the invariant is “false == false”, which is true. The number of inversions on the right is 48, because the 11 has lost two inversions, but the 14 has gained one. The blank is on an odd row from the bottom. So the value of the invariant is “true==true”, which is still true.
Proof of Fact 2
- Moving a tile along the row (left or right) doesn’t change the number of inversions and doesn’t change the row of the blank.
- Moving a tile along the column (up or down) does change the number of inversions. The tile moves past an odd number of other tiles (N – 1). So the number of inversions changes by odd number of times. The row of the blank also changes, from odd to even, or from even to odd. So both halves of the invariant changes. So its value is preserved.
Combining Fact 1 + Fact 2 = Fact 3:
- If the width is odd, then every solvable state has an even number of inversions.
If the width is even, then every solvable state has
- an even number of inversions if the blank is on an odd numbered row counting from the bottom;
- an odd number of inversions if the blank is on an even numbered row counting from the bottom;
Proof of fact 3:
- The initial (solved) state has those properties.
- Those properties are preserved by every legal move.
- Any solvable state can be reached from the initial state by some sequence of legal moves.
How to check if an instance of 8 puzzle is solvable?
This article is contributed by Aditya Goel. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.