Hello! How can I solve the towers of Hanoi but with more than three towers? Please help | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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

15th Feb 2021, 8:17 PM
Valeria Edith Almanza Mamani
Valeria Edith Almanza Mamani - avatar
15 Answers
+ 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?
15th Feb 2021, 10:05 PM
Wilbur Jaywright
Wilbur Jaywright - avatar
+ 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)
16th Feb 2021, 12:41 PM
Valeria Edith Almanza Mamani
Valeria Edith Almanza Mamani - avatar
0
Thanks!!!. I would like to use Python, C++ or Java
15th Feb 2021, 11:16 PM
Valeria Edith Almanza Mamani
Valeria Edith Almanza Mamani - avatar
0
OK, so are you solving the puzzle, or is the computer?
15th Feb 2021, 11:17 PM
Wilbur Jaywright
Wilbur Jaywright - avatar
0
It's on computer
15th Feb 2021, 11:19 PM
Valeria Edith Almanza Mamani
Valeria Edith Almanza Mamani - avatar
0
what do you want the output to be?
15th Feb 2021, 11:39 PM
Wilbur Jaywright
Wilbur Jaywright - avatar
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
15th Feb 2021, 11:55 PM
Valeria Edith Almanza Mamani
Valeria Edith Almanza Mamani - avatar
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?
16th Feb 2021, 12:25 AM
Wilbur Jaywright
Wilbur Jaywright - avatar
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
16th Feb 2021, 12:32 AM
Valeria Edith Almanza Mamani
Valeria Edith Almanza Mamani - avatar
0
can you post your working code?
16th Feb 2021, 1:03 AM
Wilbur Jaywright
Wilbur Jaywright - avatar
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?
17th Feb 2021, 2:21 PM
Wilbur Jaywright
Wilbur Jaywright - avatar
0
Yes, I arrived
18th Feb 2021, 2:29 PM
Valeria Edith Almanza Mamani
Valeria Edith Almanza Mamani - avatar
0
got lists?
18th Feb 2021, 3:10 PM
Wilbur Jaywright
Wilbur Jaywright - avatar
0
yes, that too
19th Feb 2021, 6:15 PM
Valeria Edith Almanza Mamani
Valeria Edith Almanza Mamani - avatar
0
okay, well... a couple of simple for loops with the range function should do it. have you tried that?
19th Feb 2021, 6:23 PM
Wilbur Jaywright
Wilbur Jaywright - avatar