15 Puzzle Game: Existence Of The Solution
This game is played on a board. On this board there are playing tiles numbered from 1 to 15. One cell is left empty (denoted by 0). You need to get the board to the position presented below by repeatedly moving one of the tiles to the free space:
The game "15 Puzzle” was created by Noyes Chapman in 1880.
Existence Of The Solution
Let's consider this problem: given a position on the board, determine whether a sequence of moves which leads to a solution exists.
Suppose we have some position on the board:
where one of the elements equals zero and indicates an empty cell
Let’s consider the permutation:
i.e. the permutation of numbers corresponding to the position on the board without a zero element
Let be the number of inversions in this permutation (i.e. the number of such elements and that , but ).
Suppose is an index of a row where the empty element is located (i.e. using our convention, ).
Then, the solution exists iff is even.
Implementation
The algorithm above can be illustrated with the following program code:
int a[16];
for (int i=0; i<16; ++i)
cin >> a[i];
int inv = 0;
for (int i=0; i<16; ++i)
if (a[i])
for (int j=0; j<i; ++j)
if (a[j] > a[i])
++inv;
for (int i=0; i<16; ++i)
if (a[i] == 0)
inv += 1 + i / 4;
puts ((inv & 1) ? "No Solution" : "Solution Exists");Proof
In 1879 Johnson proved that if is odd, then the solution doesn’t exist, and in the same year Story proved that all positions when is even have a solution.
However, all these proofs were quite complex.
In 1999 Archer proposed a much simpler proof (you can download his article here).