[英]Algorithm for multiple stack sorting according to pattern
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. 在业余时间,我提出了一个小型个人项目,以激励他们使用新技术(对我来说!)练习,例如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!).
河内经典递归塔似乎太基础了,而Shunting Yard和Traveling Salesman(以及标准Bubblesort,Quicksort等)之类的算法似乎不太适合(如果我错了,请纠正我!)。
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. 正如我所提到的,最后我很可能会使用Python或JS来实现这一点,但是在此阶段,任何想法或摘要都值得赞赏-我希望稍后自己进行开发工作。
Thanks in advance! 提前致谢!
Alan 艾伦
One method would be to first move the 3 unused blocks to S4. 一种方法是首先将3个未使用的块移到S4。 Setup the distribution so it's S1 = 3 blocks, S2 = 3 blocks, S3 = 2 blocks.
设置分布,使其为S1 = 3个块,S2 = 3个块,S3 = 2个块。 If S3 has 1 or 2 unused blocks, it takes 1 or 2 moves to move the unused block(s) to S4.
如果S3有1或2个未使用的块,则需要1或2步将未使用的块移动到S4。 Repeat for S2, taking 1 to 3 moves to move 1 to 3 unused blocks.
对S2重复上述步骤,以1到3步移动1到3个未使用的块。 Unused blocks are removed from S1 last.
最后将未使用的块从S1中删除。
Now you have 5 blocks and 3 stacks. 现在您有5个方块和3个堆栈。 Say S1 has 3 blocks and S2 has 2 blocks.
假设S1有3个块,S2有2个块。 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.
属于S1顶部的块可以采取最多的移动来结束S1仅有的块。 For example, the block that belongs at the top of S1 is at the top of S2.
例如,属于S1顶部的块位于S2的顶部。 So move 2 blocks from S2 to (now empty) S3, so the key block is at the bottom of S3.
因此将2个块从S2移到S3(现在为空),因此关键块位于S3的底部。 Then move all 3 blocks from S1 to S2, then the key block to S1.
然后将所有3个块从S1移动到S2,然后将关键块移动到S1。 After that, the problem becomes 4 blocks and 3 stacks of size 4, 3, 3, which shouldn't be too difficult.
之后,问题就变成了4个块和3个大小为4、3、3的堆栈,这应该不太困难。 Once this is done, then the 3 blocks in S4 can be moved to S2 or S3.
完成此操作后,即可将S4中的3个块移至S2或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. 对于3栈排序,多相合并排序最快,但是所有3栈的大小必须相同,因此此处不适用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.