0
Hello! How can I solve the towers of Hanoi but with more than three towers? Please help
that is, from the beginning, the user would enter n disks and n towers
15 odpowiedzi
+ 3
just looking at the puzzle myself, it seems rather easy. One simply uses one rod as a buffer to place the entire tower upside down, and then moves it to the target rod thus flipping it back right side up.
to implement this in computer seems even simpler with FIFO stacks, but maybe im missing something. what language are you wanting to use?
+ 1
Clear ! this is my code:
def hanoi (n, start_pivot, end_pivot, auxiliary_pivot):
if n == 1:
print (start_pivot + "->" + end_pivot)
else:
hanoi (n-1, start_pin, auxiliary_pin, end_pin)
print (start_pivot + "->" + end_pivot)
hanoi (n-1, auxiliary_pivot, end_pin, start_pin)
0
Thanks!!!. I would like to use Python, C++ or Java
0
OK, so are you solving the puzzle, or is the computer?
0
It's on computer
0
what do you want the output to be?
0
something like that :
move disk 0 to 2
move disk 0 to 1
move disk 2 to 1
move disk 0 to 2
move disk 1 to 0
move disk 1 to 2
move disk 0 to 2
0
Okay. now to solving: as I mentioned before, it seems like a matter of just moving the entire tower to one pole, and then to the target pole. Is this not the case?
0
Thank you, it sure is like that, could you help me? I only did for three towers in Python, and I'm getting a bit confused doing for more towers
0
can you post your working code?
0
ok. well, it looks like in trying to be more efficient, youve actually painted yourself into a corner where you need to create a new if statement for EVERY situation. Have you gotten to “for” loops in python yet?
0
Yes, I arrived
0
got lists?
0
yes, that too
0
okay, well...
a couple of simple for loops with the range function should do it. have you tried that?