23
1
Problem
Say you have N stacks named S1 through SN, where each Sk (k = 1 to N) contains N copies of the number k.
For example, when N = 3 the stacks looks like this:
1 2 3 <- top of stack
1 2 3
1 2 3 <- bottom of stack
=======
1 2 3 <- stack index
Here there are 3 stacks indexed as 1, 2, and 3, and each one contains N instances of its own index.
The goal is to rearrange the N stacks such that each of them identically contains the numbers 1 through N in order from top to bottom.
e.g. for N = 3 the goal is to rearrange the stacks into:
1 1 1
2 2 2
3 3 3
=======
1 2 3
The only action you can perform with the stacks is taking the top number from one of the stacks (popping) then immediately placing it on top of a different stack (pushing). This is subject to these stipulations:
A number can only be pushed onto a stack if it is less than or equal to the top number on that stack.
e.g. a
1
can be pushed onto a stack with a1
,2
, or3
at the top, but a2
can only be pushed onto a stack with a2
or3
(or higher) at the top.This has the effect that stacks are always monotonically increasing from top to bottom.
Any nonempty stack may be popped from, and, assuming the previous bullet is satisfied, any stack may be pushed to.
Any number may be pushed onto an empty stack.
Stacks have no maximum height limit.
Stacks cannot be created or destroyed, there are always N of them.
This challenge is about deciding which pops and pushes to do in order to complete the stack exchange, not necessarily in the fewest moves, but in a surefire way.
(Practicing with a deck of cards is a good way to get a feel for the problem.)
Challenge
Write a program or function that takes in a positive integer N, guaranteed to be 3 or above. Print or return a string that denotes all the pop-push actions required to rearrange the stacks from the initial state:
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
=============
1 2 3 4 5
(N = 5 case)
To the final state:
1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
4 4 4 4 4
5 5 5 5 5
=============
1 2 3 4 5
Every line in your output must contain two numbers separated by a space. The first number is the index of the stack to pop from and the second number is the index of the stack to push to. Performing the actions of all the lines in order should arrange the stacks correctly without breaking any rules.
For example, here is a potential valid output for the N = 3 case:
1 2 [move the top number on stack 1 to the top of stack 2]
1 2 [repeat]
1 2 [repeat]
3 1 [move the top number on stack 3 to the top of stack 1]
2 3 [etc.]
2 3
2 3
2 1
2 1
2 1
3 1
3 1
3 1
3 2
1 2
1 2
1 2
1 3
2 3
2 3
2 3
1 2
3 2
3 1
Notes
Your output does not need to be optimal, only correct. i.e. you do not need to minimize the number of pops and pushes.
- So it would be alright if, say, some move were repeatedly made and immediately reversed.
- Popping and pushing to the same stack in one move, e.g.
2 2
, is allowed as well (though of course pointless).
Your output does need to be deterministic and finite.
Remember that stacks have 1-based indexing. 0-based indexing is not allowed.
N greater than 9 should of course work just as well as single digit N.
If desired you may use any two distinct, non-digit printable ASCII characters in place of spaces and newlines. A trailing newline (or newline substitute) in the output is fine.
Scoring
The shortest code in bytes wins. Tiebreaker is higher voted answer.
Valueless brownie points if you can show your algorithm is optimal.
Stop with the "extra points for small things" nonsense >_> – user48538 – 2016-05-30T17:02:33.173
18@zyabin101 You just lost any chance at brownies. – Calvin's Hobbies – 2016-05-30T17:04:53.970
9You always come up with such wonderful titles! – Luis Mendo – 2016-05-30T17:27:44.967
@HelkaHomba
-._(._.)_.-
– user48538 – 2016-05-30T18:18:10.733Is the possible output you include for the case of
N=3
optimal? – R. Kap – 2016-06-05T23:17:52.010