There is a well-known method using steps (1) Knuth or similar shuffle (2) test for validity (3) make one more swap if needed. This amounts to 3 separate algorithms, and (because of step 3) does not produce every possible solvable puzzle with exactly equal probability.
I think your conclusion here is faulty. Here's a 3-step method:
- Shuffle the grid normally.
- Check if the grid is solvable.
- If the grid isn't solvable, then swap the tile labeled 1 with the tile labeled 2.
First of all, there is a bijection between solvable grids and unsolvable ones, obtained by swapping tiles 1 and 2. Clearly, the shuffle in step (1) produces all grids with equal probability, but only half of these are solvable. So if we apply the bijection to all unsolvable grids, then we're left with a uniform distribution over solvable grids. (There are exactly 2 ways to "end" with any given solvable grid: either shuffle the grid into that state, or shuffle the grid into its unique corresponding unsolvable state. Clearly the probability of this is the same for every solvable grid.)
Proof of correctness
From the Wikipedia article on the 15 puzzle:
The invariant is the parity of the permutation of all 16 squares plus the parity of the taxicab distance (number of rows plus number of columns) of the empty square from the lower right corner. This is an invariant because each move changes both the parity of the permutation and the parity of the taxicab distance. In particular if the empty square is in the lower right corner then the puzzle is solvable if and only if the permutation of the remaining pieces is even.
From the Wikipedia article on the parity of a permutation:
An even permutation can be obtained as the composition of an even number and only an even number of exchanges (called transpositions) of two elements, while an odd permutation can be obtained by (only) an odd number of transpositions.
Hence, swapping (transposing) any two numbered tiles (not the empty square) will change the parity of the permutation (we added one transposition). However, the parity of the taxicab distance of the empty square is unchanged (since the empty square did not move).
Therefore, such an operation will change the solvability of the puzzle. In other words, applying this operation (e.g., swapping tiles labeled 1
and 2
) will always transform an unsolvable puzzle into a solvable one and vice versa. (This operation is also a bijection because it is invertible.)
Proof that requirements (b) and (c) cannot be met simultaneously
Consider the starting 2x2 grid:
1 2
3 _
and the "goal" grid:
_ 3
2 1
Can we move from the first grid to the second grid, using at most 4 swaps (requirement (b)) and keeping the grid solvable after each swap (requirement (c))?
First, notice that the only allowable swap satisfying requirement (c) is to move the space _
with an adjacent tile (not the diagonal tile). By symmetry, we can just move the space clockwise without loss of generality. Performing any counter-clockwise swap is then useless since it cancels out the previous clockwise swap.
So we repeatedly swap the space with the adjacent tile in the clockwise direction until we obtain the desired grid. This takes 6 swaps. Since 6 > 4, requirement (b) cannot be met while satisfying requirement (c).