What is 8 puzzle?
Given a 3×3 board with 8 tiles (every tile has one number from 1 to 8) 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.

puzzle

How to find if given state is solvable?
Following are two examples, the first example can reach goal state by a series of slides. The second example cannot.

puzzle2

Following is simple rule to check if a 8 puzzle is solvable.
It is not possible to solve an instance of 8 puzzle if number of inversions is odd in the input state. In the examples given in above figure, the first example has 10 inversions, therefore solvable. The second example has 11 inversions, therefore unsolvable.

[ad type=”banner”]

What is inversion?
A pair of tiles form an inversion if the the values on tiles are in reverse order of their appearance in goal state. For example, the following instance of 8 puzzle has two inversions, (8, 6) and (8, 7).

1 2 3
4 _ 5
8 6 7
Following is a simple C++ program to check whether a given instance of 8 puzzle is solvable or not. The idea is simple, we count inversions in the given 8 puzzle.

[pastacode lang=”cpp” manual=”%2F%2F%20C%2B%2B%20program%20to%20check%20if%20a%20given%20instance%20of%208%20puzzle%20is%20solvable%20or%20not%0A%23include%20%3Ciostream%3E%0Ausing%20namespace%20std%3B%0A%20%0A%2F%2F%20A%20utility%20function%20to%20count%20inversions%20in%20given%20array%20’arr%5B%5D’%0Aint%20getInvCount(int%20arr%5B%5D)%0A%7B%0A%20%20%20%20int%20inv_count%20%3D%200%3B%0A%20%20%20%20for%20(int%20i%20%3D%200%3B%20i%20%3C%209%20-%201%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20for%20(int%20j%20%3D%20i%2B1%3B%20j%20%3C%209%3B%20j%2B%2B)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20Value%200%20is%20used%20for%20empty%20space%0A%20%20%20%20%20%20%20%20%20%20%20%20%20if%20(arr%5Bj%5D%20%26%26%20arr%5Bi%5D%20%26%26%20%20arr%5Bi%5D%20%3E%20arr%5Bj%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20inv_count%2B%2B%3B%0A%20%20%20%20return%20inv_count%3B%0A%7D%0A%20%0A%2F%2F%20This%20function%20returns%20true%20if%20given%208%20puzzle%20is%20solvable.%0Abool%20isSolvable(int%20puzzle%5B3%5D%5B3%5D)%0A%7B%0A%20%20%20%20%2F%2F%20Count%20inversions%20in%20given%208%20puzzle%0A%20%20%20%20int%20invCount%20%3D%20getInvCount((int%20*)puzzle)%3B%0A%20%0A%20%20%20%20%2F%2F%20return%20true%20if%20inversion%20count%20is%20even.%0A%20%20%20%20return%20(invCount%252%20%3D%3D%200)%3B%0A%7D%0A%20%0A%2F*%20Driver%20progra%20to%20test%20above%20functions%20*%2F%0Aint%20main(int%20argv%2C%20int**%20args)%0A%7B%0A%20%20int%20puzzle%5B3%5D%5B3%5D%20%3D%20%20%7B%7B1%2C%208%2C%202%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B0%2C%204%2C%203%7D%2C%20%20%2F%2F%20Value%200%20is%20used%20for%20empty%20space%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B7%2C%206%2C%205%7D%7D%3B%0A%20%20isSolvable(puzzle)%3F%20cout%20%3C%3C%20%22Solvable%22%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20cout%20%3C%3C%20%22Not%20Solvable%22%3B%0A%20%20return%200%3B%0A%7D” message=”C++ Program” highlight=”” provider=”manual”/]

Output:

Solvable
Note that the above implementation uses simple algorithm for inversion count. It is done this way for simplicity. The code can be optimized to O(nLogn) using the merge sort based algorithm for inversion count.

[ad type=”banner”]

How does this work?
The idea is based on the fact the parity of inversions remains same after a set of moves, i.e., if the inversion count is odd in initial stage, then it remain odd after any sequence of moves and if the inversion count is even, then it remains even after any sequence of moves. In the goal state, there are 0 inversions. So we can reach goal state only from a state which has even inversion count.

How parity of inversion count is invariant?
When we slide a tile, we either make a row move (moving a left or right tile into the blank space), or make a column move (moving a up or down tile to the blank space).
a) A row move doesn’t change the inversion count. See following example

   1   2   3    Row Move     1   2   3
   4   _   5   ---------->   _   4   5 
   8   6   7                 8   6   7
  Inversion count remains 2 after the move

   1   2   3    Row Move     1   2   3
   4   _   5   ---------->   4   5   _
   8   6   7                 8   6   7
  Inversion count remains 2 after the move

b) A column move does one of the following three.
…..(i) Increases inversion count by 2. See following example.

         1   2   3   Column Move     1   _   3
         4   _   5   ----------->    4   2   5  
         8   6   7                   8   6   7
      Inversion count increases by 2 (changes from 2 to 4)

…..(ii) Decreases inversion count by 2

         1   3   4    Column Move     1   3   4
         5   _   6   ------------>    5   2   6
         7   2   8                    7   _   8
      Inversion count decreases by 2 (changes from 5  to 3)

…..(iii) Keeps the inversion count same.

         1   2   3    Column Move     1   2   3
         4   _   5   ------------>    4   6   5
         7   6   8                    7   _   8
        Inversion count remains 1 after the move

So if a move either increases/decreases inversion count by 2, or keeps the inversion count same, then it is not possible to change parity of a state by any sequence of row/column moves.

[ad type=”banner”]