python - Tower of Hanoi with adjacent restriction -


Resolved with impending restriction. I tried to look around but I could not find any clue.

What have I tried so far:

  DH Hanoi (N, Source, Helper, Target): Printed "Hanoi [0]: If the source [0] [- 1] is called, "Source, Assistant, Goal," if for the "source" if n & gt; 0: Hanoi (N-1, Source, Assistant, Goal) == 1: Move (source, subsidiary) go (assistant, goal) Second: go to Hanoi (N-1, target, assistant, source) (source, assistant) Hanoi (N-1, Assistant, Goal, Source) Hanoi (N-1, Source, Assistant, Target) Def L (S, D): Disk = s [0] .pop () print "moving" + ar (disk) + ["]" from [1] + "to" + d [1] d [0]. Target = ([], "source") target = ([], "target") helper = ([], "helper") Hanoi (lane (source [0]), source, assistant , Target)   

which only works for 2 discs. Thanks

I got this good math description

< Div class = "post-text" itemprop = "text">

a verbose implementation

At first glance it seems as if you had to distinguish the case where you You can go directly from the current source to the current target, and in those two steps where you have to move the largest disk, the following implementation does this:

  class pile (object): Def __init __ (self, index, name, disk): self.index = index self.name = name self.disks = disc def __str __ (self): return self-name df __repr __ (self): returns' stack % R,% R,% R) '% (Self and Index, Spec. Name, self-disks) def is_adjacent (self, other): back to other Index (self index + 1, self index-1) return DEF push (self, disk): lane (self-disks) == 0 or self-disks [-1] & gt; Disk self.disks.append (disc) def pop (self): return self.disks.pop () class Hanoi (object): def __init __ (self, n): source = stack (0, "source", range ( Target = stack (2, "target", []) self.stacks = [source, helper, goal] self.hanoi (n, n, 0, -1) helper = stack (1, "helper", [ Source, goal) DH Hanoi (self, n, source, goal): "" transfer from "steak" and target remaining stacks using "assist" = self.stacks [3 - source.index - target.index] N if n == 0: returns if source.is_adjacent (target): self.hanoi (n - 1, source, helper) self.move (sources , Goal) self.How (self-help, target): self-help (source, goal) self.move (source, target) and helper.is_adjacent (target) self.hanoi (n - 1, source, goal) Self.hanoi (n - 1, goal, source) self.move (accessory, goal) self.hanoi (n - 1, source, goal) def moves (self, s, d): sisaidadent (d) Disk = s.pop () from "% s to% s% (disk, s, d) d. Push (disk) hanoi (5)   

stack < Code> Keep all the things about one of the objects in one of your stacks: name, position If you add third element to catch that index then you can use tuples, but I understand OOP more intuitive.

Hanoi puts the set of square stack together. It only allows the Hanoi method to specify the source and target, while the third stack is approximate. You can code it differently, but I think it is very easy to understand the recurring call except the helper: now the single-disc run and multi-disk both Hanoi For> You specify the source and destination, and there is no third stack.

Room for simplicity

Now if you keep an eye out, you will find that recurring calls are always for non-adjacent stack. So if your stack order is really such that the sources and goals are non-imminent, then all recursive calls will use the else of my code branch, and you can avoid things However, in the context of what is happening, I have got more verbatim code above to understand.

Comments

Popular posts from this blog

excel vba - How to delete Solver(SOLVER.XLAM) code -

github - Teamcity & Git - PR merge builds - anyway to get HEAD commit hash? -

ios - Replace text in UITextView run slowly -