1. Three cards
Even though it would seem not too many things can be done with just three cards, there are actually 1.64 × 1018 memoryless algorithms to try, with most not working. You need brainpower. With some reasoning and experimentation, you may find that moving the cards mostly in one direction (such as to the right, including "around the corner" from the right stack to the left stack) helps avoid looping. Moreover, you generally want to expose more cards, except perhaps in some positions from which you might win. Of the several algorithms that do work, I like the following one, as suggested by my mathematics Ph.D. student Ewa Infeld, given by these four rules in priority order:
2. Any number of cards.
This same algorithm works for any number of cards, numbered 1 through n. It takes quadratic time in the worst or random case, or approximately a constant times n2 steps.
3. With just one card in the middle.
The following algorithm was submitted by Takashi Chiba in response to a puzzle column published in Japan, and communicated to me by Ko Sakai of the Graduate School of Pure and Applied Sciences, University of Tsukuba.
This algorithm is cubic, requiring (2/3)n3 + (1/2)n2 (7/6)n steps if started with the cards stacked in reverse order on the left. Once started, it never has more than one card in the center. We thus see sorting can be done with only two LIFO stacks, a single register, and no memory. If you can beat that, please let me know...
All are encouraged to submit prospective puzzles for future columns to firstname.lastname@example.org.
The Digital Library is published by the Association for Computing Machinery. Copyright © 2014 ACM, Inc.
I believe the following is quadratic and has not more than one card in the middle -
Call the stacks (and their top cards) A, B, and C
If B is empty:
if C is empty move A to B
elIf A is empty or A > C move C to A
else move C to B
If B is not empty:
If A is empty move B to A
If C is not empty and A > B > C move B to A
else move C to A
Intuitively, search left to right for an out of order card, then search right to left for a place to insert it.
Once all are sorted, the left to right search forces everything into the leftmost stack.
Displaying 1 comment