Reputation: 2517
In my (little) spare time, I've come up with a small personal project as motivation to practice using new technologies (for me!), such as Python 3 & Node JS.
I want to try to create a solution routine for a puzzle, similar to Towers of Hanoi in construction, as shown in the following image:
Puzzle Rules
Algorithms:
I've been searching for a while for a best-fit multi-stack 'sorting' algorithm based on a pattern, to avoid re-inventing the wheel, but can't find anything that seems relevant, although I may be looking in the wrong place. Towers of Hanoi classic recursion appears too basic, while algorithms like Shunting Yard and Travelling Salesman (and standard Bubblesort, Quicksort, etc.) don't seem a good fit (please correct me if I'm wrong!).
My question:
Can anyone suggest or recommend a suitable base algorithm that I could build on or adapt for solving this puzzle? It will probably need to be recursive, to find the correct paths, but I'm sure this type of multi-stack or pattern-based sorting has been done before somewhere? I eventually want to attain the best efficiency thru minimum number of steps required, but that comes later.
As I mentioned, it's most likely that I'll use Python or JS to implement this in the end, but any ideas or snippets are appreciated at this stage - I expect to do the dev work myself later on.
Thanks in advance!
Alan
Upvotes: 0
Views: 546
Reputation: 28921
One method would be to first move the 3 unused blocks to S4. Setup the distribution so it's S1 = 3 blocks, S2 = 3 blocks, S3 = 2 blocks. If S3 has 1 or 2 unused blocks, it takes 1 or 2 moves to move the unused block(s) to S4. Repeat for S2, taking 1 to 3 moves to move 1 to 3 unused blocks. Unused blocks are removed from S1 last.
Now you have 5 blocks and 3 stacks. Say S1 has 3 blocks and S2 has 2 blocks. The block that belongs at the top of S1 could take the most number of moves to end up where that is the only block S1 has. For example, the block that belongs at the top of S1 is at the top of S2. So move 2 blocks from S2 to (now empty) S3, so the key block is at the bottom of S3. Then move all 3 blocks from S1 to S2, then the key block to S1. After that, the problem becomes 4 blocks and 3 stacks of size 4, 3, 3, which shouldn't be too difficult. Once this is done, then the 3 blocks in S4 can be moved to S2 or S3.
As for a 3 stack sort, a polyphase merge sort is fastest, but all 3 stacks need to be the same size, so that doesn't apply here.
Upvotes: 1