问题
The problem is to generate 'fifteen' puzzles in varying sizes, ensuring that all puzzles are solvable. For testing a solvable puzzle, see Wikipedia or Wolfram.
Naive shuffle methods include (a) simulate a large number of random moves (b) shuffle somehow, check if valid, fix if not. See also How can I ensure that when I shuffle my puzzle I still end up with an even permutation?.
An optimal shuffle will (a) produce every possible solvable puzzle with equal probability (b) perform no more than N swaps, where N is the number of tiles, and most likely (c) leave the puzzle solvable after every swap.
Is there such a method?
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. If there is no method that satisfies the requirements, then there should be a proof to that effect.
After further thought, step 3 may not harm the distribution of shuffles but it does (a) make the total number of shuffles uncertain (b) require an additional test. In the target application, the logic to do that test is not available.
So the question could be reformulated as finding a minimum set of swaps that generate every possible valid shuffle with equal probability. The final result must be a solvable puzzle with no need to test for solvability. Hypothetically the number of swaps needed is N-1.
回答1:
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).
回答2:
Assume a checkerboard colouring. Then starting with a solvable puzzle (permutation parity even) the following swaps are possible:
- Any two numbers: parity is reversed.
- A space with any number on a square of the same colour: parity is reversed.
- A space with any number on a square of the opposite colour: parity is unchanged.
The puzzle can only be made valid at every step (parity even) if the only swaps are of type 3. If each swap of type 1 or 2 is followed by another of the same type then the puzzle will be valid after each such pair.
A sufficient number of swaps chosen randomly from the above will achieve a high degree of disorder. A total of 16 swaps, such that every square is swapped at least once and obeying these swap rules should be sufficient. It is hard to determine whether the distribution of puzzles is perfect, but it should be good enough for most purposes.
The solution generalises easily to puzzles other than 4x4.
回答3:
Assume a checkerboard colouring, white top left. Then starting with a solvable puzzle the following swaps keep the puzzle solvable:
- A space in the 1st or 3rd row with any square of the same colour
- A space in the 2nd or 4th row with any square of the opposite colour
- Any two numbers on squares of the same colour.
16 valid swaps chosen randomly from the above, one per square, will achieve a high degree of disorder and a valid puzzle. I cannot determine whether the distribution of puzzles is perfect, but it should be sufficient for most purposes.
The solution generalises to puzzles other than 4x4.
来源:https://stackoverflow.com/questions/50809644/how-to-optimally-shuffle-the-fifteen-puzzle-for-all-sizes