The 15 puzzle has fifteen numbered tiles and one empty square on a 4 by 4 board, and a move slides a tile into the empty square next to it. Suppose the tiles are in order except that 14 and 15 have changed places. It looks like a small repair, yet no sequence of slides, however long, puts them back. This page finds a quantity that no slide can change and uses it to tell the two positions apart without making a single move.
Key terms
15 puzzle
Definition The 15 puzzle is the sliding puzzle on a 4 by 4 board with fifteen tiles numbered to and one empty square, called the blank. A legal move slides a tile that shares a side with the blank into the blank. In the goal position the tiles read to from left to right along the rows, top row first, and the blank is in the bottom right corner. The impossible fifteen puzzle starts from the goal position with tiles and exchanged and asks whether legal moves can reach the goal. They cannot. Read the tiles row by row in the same order, skipping the blank, let be the number of inversions in this list, and let be the row of the blank counted from the bottom, so that the bottom row is row . A horizontal move changes neither the list nor . A vertical move changes by and moves one tile past exactly three others in the list, which changes by or , up or down, so changes by an even number. Whether is even or odd therefore never changes, and the two positions give
One is odd and the other even, so the goal cannot be reached from the start.
In plain words Every position carries a hidden label, even or odd, and sliding tiles never changes it. The goal is labeled odd and the swapped position is labeled even, so no amount of sliding connects them.
Example From the goal, slide tile to the right into the blank. The list is still to and the blank is still in row , so . From the goal, slide tile down instead. In reading order the square of a tile and the square directly below it are four places apart, with three squares between them, and here those squares hold , and . So moves from before these three tiles to after them, and the list ends 11, 13, 14, 15, 12. It has the three inversions formed by with , and , the blank has moved to row , and , which is odd again.
Sliding puzzle
Definition A sliding puzzle on a board with rows and columns, where and , has tiles numbered to and one blank square. A legal move slides a tile that shares a side with the blank into the blank, so the tile and the blank trade places. A position records where every tile and the blank are, so there are positions, where is the product . The goal position has the tiles in increasing order, read along the rows from the top, with the blank in the bottom right corner. Every move can be undone by sliding the same tile back, so if a position can be reached from a position , then can be reached from .
In plain words Tiles move one at a time into the single empty space, and any move can be reversed by sliding the tile back.
Example On a 2 by 2 board with tiles , and there are positions. The blank can only travel around the ring of four squares, and each move shifts one tile one step along the ring, so the order of the tiles read clockwise around the ring never changes. In the goal, with and in the top row and at the bottom left, the tiles read , , clockwise, and so they do in every position reached from it. There are exactly such positions, since the blank can be in any of the four squares and tile in any of the other three, and all of them can be reached. Exchanging tiles and in the goal gives one of the other positions, which can never be solved.
Inversion
Definition In a list of different numbers, an inversion is a pair of positions with , that is, a larger number that comes before a smaller one. A list in increasing order has no inversions. A list in decreasing order has every pair inverted, so it has the largest possible number,
In plain words Count the pairs of numbers that are in the wrong order relative to each other, wherever they stand in the list.
Example The list 1, 3, 2 has one inversion, formed by and . The list 3, 1, 2 has two, since comes before both and , while and are in the right order. The list 4, 3, 2, 1 has all of its pairs inverted. In the 15 puzzle with and exchanged, the list read row by row is to followed by and , so its only inversion is formed by and , and .
Permutation parity
Definition A permutation of the numbers to is a list that contains each of them exactly once. Its parity is even when its number of inversions is even, and odd when that number is odd. Exchanging any two entries of a permutation changes its parity. Exchanging two neighboring entries changes the order of exactly one pair, so it changes the number of inversions by . Exchanging the entries in positions can be done by exchanges of neighbors, an odd number, so it changes the parity too. As a result, every way of sorting a permutation by exchanges uses an even number of exchanges if the permutation is even, and an odd number if it is odd.
In plain words A single swap of two entries always flips a permutation between even and odd. A list that is one swap away from sorted can never be sorted with an even number of swaps.
Example The permutation 3, 1, 2 is even, with two inversions, and it is sorted by two exchanges, first of and , which gives 1, 3, 2, and then of and . The list of tiles in the swapped 15 puzzle is odd, one exchange away from to . In the puzzle, a position with the blank back in the bottom right corner has , so it can be reached from the goal only if is even. Every position reached from the goal with the blank in that corner therefore has its tiles in an even permutation, and the swapped position, whose permutation is odd, is not one of them.
Unsolvable position
Definition A position of a sliding puzzle is unsolvable if no sequence of legal moves leads from it to the goal position. On the 4 by 4 board, with and as above, every position with even is unsolvable, because is odd at the goal and its parity never changes. It is also a known theorem, with a longer proof, that every position with odd can be solved, so exactly half of the positions are solvable. On a board with an odd number of columns, a vertical move passes an even number of tiles and changes by an even number, so there the parity of alone is the invariant, and a position is unsolvable when is odd.
In plain words A position is unsolvable when it carries the wrong label. On the 4 by 4 board the label is whether the number of inversions plus the row of the blank is even or odd.
Example Reverse the tiles, so that they read , and so on down to row by row, with the blank in the bottom right corner. Every pair is inverted, so
and with the sum is even, so this position is unsolvable. Exchanging any two of its tiles leaves as it is and changes the parity of , so the new position has odd and, by the theorem, can be solved. The board has positions, and of them are solvable.
Check yourself
- Start from the goal and slide tile down, then tile to the right, then tile down. Find and after each move and check that stays odd.
Show the answer
After tile moves down, the list ends 11, 13, 14, 15, 12, so , and the blank is in the third row from the top, which is row from the bottom. The sum is . Sliding to the right is a horizontal move, so the list and the row stay the same, and the sum is still .
Tile then moves from the second row into the blank below it, and in reading order it passes , and . The list becomes
with three inversions formed by and three formed by , so . The blank is now in the second row from the top, which is row from the bottom, and the sum is . All three sums are odd, as the invariant requires.
- Put the blank in the top left corner and the tiles to in order after it, reading row by row. The list of tiles has no inversions, just like the goal. Can this position be solved?
Show the answer
No. The list is to in order, so , but the blank is in the top row, which is row from the bottom. Then is even, while the goal has , which is odd, so no sequence of legal moves reaches the goal. Counting inversions alone would wrongly suggest that this position is as good as solved. On a board with four columns the row of the blank is part of the invariant and cannot be left out.
- Change the board. On a 3 by 3 board with tiles to and the goal in the same reading order, slide tile down from the goal and compute . Then find the invariant for this board and decide whether the goal position with and exchanged can be solved.
Show the answer
After the slide the rows read 1, 2, 3 and 4, 5 with the blank at the right, and then 7, 8, 6. The list is 1, 2, 3, 4, 5, 7, 8, 6, with two inversions formed by with and , so . The blank is in row from the bottom, so is even, although the position is one move from the goal. The rule for the 4 by 4 board does not hold here.
On a board with three columns, a tile moving vertically passes only the two tiles between its old and new squares in reading order. Each of those two pairs changes between inverted and not inverted, so changes by , or , and a horizontal move does not change at all. The parity of alone is therefore the invariant. The goal has , which is even, and the position with and exchanged has , which is odd, so it cannot be solved.