简体   繁体   English

根据模式对多个堆栈进行排序的算法

[英]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 拼图规则

  1. 8 coloured blocks are randomly positioned on 3 stacks (S1-S3), which have capacity 5, 3, 3 respectively. 8个有色块随机放置在3个堆栈(S1-S3)上,分别具有5、3、3个容量。
  2. A fourth stack (S4) of size 3 is empty, but is used as transient storage for movements between the other stacks; 大小为3的第四个堆栈(S4)为空,但用作其他堆栈之间移动的临时存储; eg to move blue next to purple in S2: Move red [S2 -> S4], move green [S2 -> S4], move blue [S3 -> S4], move blue [S4 -> S2], etc. 例如,在S2中将蓝色移动到紫色旁边:移动红色[S2-> S4],移动绿色[S2-> S4],移动蓝色[S3-> S4],移动蓝色[S4-> S2],依此类推。
  3. A target pattern of 5 blocks is randomly generated, and the blocks must be moved between stacks one by one (via S4) until the correct ordered blocks are present in S1, at which point S4 must again be empty (the final state of S2, S3 is not important). 随机生成5个块的目标模式,并且必须逐个在堆栈之间移动这些块(通过S4),直到在S1中存在正确的有序块为止,此时S4必须再次为空(S2的最终状态, S3不重要)。
  4. Note that my illustration is not perfect, and that random scenario is actually more challenging! 请注意,我的插图并不完美,随机场景实际上更具挑战性! However, imagine that all stacks are accessed at their base, so first accessible block on S1 (left) is the black one, etc. 但是,假设所有堆栈都在其基础处访问,那么S1(左)上的第一个可访问块为黑色,依此类推。
  5. There are some tips for solutions that could eventually be coded into the algorithm, eg focus on last block first, in this case purple. 有一些解决方案的技巧可以最终编码到算法中,例如,首先关注最后一块,在这种情况下为紫色。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM