UPDATED RESPONSE:
Before introducing this algorithm, I need to define two terms: inversion and polarity.
Inversion: a pair of objects that are in the opposite order from where they should be. For more information on inversions, see Counting Inversions in an Array
The polarity of the puzzle is whether the total number of inversions among all fragments is even or odd. A puzzle with 10 inversions even has polarity; a 7 inversion puzzle has an odd polarity.
Consider a 3x3 puzzle as follows:
| 6 | 3 | 2 |
| .. | 4 | 7 |
| 5 | 1 | 0 |
Having calculated all the inversions here, we get: (i) 6 is inverted from 0, 1, 2, 3, 4 and 5. (ii) 3 is inverted from 0, 1 and 2. (iii) 2 is inverted from 0 and 1. (iv) 4 is inverted from 0 and 1. (v) 7 is inverted from 0, 1 and 5. (vi) 5 is inverted from 0 and 1. (vii) 1 is inverted from 0. In total, we have 19 .
If the width of the puzzle is an even number, then moving the tile up or down will change the polarity, so it is important that the puzzle even has polarity when the empty tile is in the last row. To do this, we will add the distance from the empty tile from the bottom line to our common inversions.
Now we know that the puzzle is solvable, it even has polarity (or permutations). Therefore, if our polarity is even then, our problem is solved, but for odd polarity we must do this:
If the empty tile is not in the first row, replace the first two adjacent tiles in the first row. This will change the polarity by 1, and we will have a resolved puzzle that even has polarity.
But if the empty tile is in the first row, then swap adjacent tiles in the last row. This would make the puzzle solvable. Therefore, in the end you always get a solvable puzzle.
Hope I answer stackoverflow requirements for this question. Any doubts are welcome. Thanks.