User:Jeff/ACM/cNteSahruPfefrlefe

Back to ACM page

cNteSahruPfefrlefe
Preston Digitation is a magician who specializes in card tricks. One thing Preston cannot get just right is perfect in-shuffles. A perfect in-shuffle is one where a deck of 52 cards is divided in half and then the two halves are perfectly interleaved so that the top card of the lower half of the deck becomes the top card of the shuffled deck. If we number the cards 0 (top) to 51 (bottom), the resulting deck after a perfect in-shuffle will look like the following: 26 0 27 1 28 2 29 3 30 4 31 5 32 6 ... 51 25

Preston finds that he makes at most one mistake per shuffle. For example, cards 2 and 28 might end up interchanged, resulting in a shuffled deck that looks like this: 26 0 27 1 2 28 29 3 30 4 31 5 32 6 ... 51 25

These exchanges of two adjacent cards are the only mistakes Preston makes. After one shuffle, it is easy for him to see if and where he has made a mistake, but after several shuffles this becomes increasingly difficult. He would like you to write a program that can determine his mistakes (if any).

Input
Input will consist of multiple problem instances. The first line will be a single integer indicating the number of problem instances. Each problem instance will consist of a single line containing the cards of a deck which has been shuffled between 1 and 10 times. All decks will be of size 52.

Note: The sample input below shows multiple lines for a problem instance. The actual input data for a problem instance is contained on a single line.

Output
For each problem instance, output the case number (starting at 1), followed by the number of shuffles that were used for that instance. If there were no mistakes made during the shuffling, output the line Otherwise, output a set of lines of the form where n is a shuffle where an error occurred and m is the location of the error. Shuffles are numbered starting with 1 and the location value should indicate the first location of the two cards that were swapped in that shuffle (where the top of the deck is position 0). In the example described above, the cards in positions 4 and 5 (the cards numbered 2 and 28) are incorrect, so m would be 4 in this case. List all errors in order of increasing n. If one or more shuffles have no errors, do not print any line for them. If there are multiple solutions, pick the solution with the fewest number of errors (all test cases will have a unique solution of minimum size).

Analysis
We didn't solve this problem at world finals, but Steve and John worked on it for a while and the three of us came up with some insights. First off, it's easy to calculate the number of shuffles in the input. For each possible number of shuffles (1 &le; n &le; 10), there can be at most 2n deviations in the input when compared to the canonical ordering (shuffling the sorted deck n times without making any mistakes). Exactly one value of n will match these criteria, since the canonical orderings are so different for each number of shuffles.

Once you know the number of shuffles, you have to figure out the locations of the mistakes. The best strategy is to work backward from the input, "unshuffling" the cards at each step (possibly making a single mistake in the process) until the deck returns to sorted order. I recommend using an A* search, where the search heuristic is the number of mistakes left to fix. We can estimate this value by dividing the number of deviations against the canonical ordering by two.