D
D
dimaQd2021-05-01 01:01:23
Algorithms
dimaQd, 2021-05-01 01:01:23

How to solve Tower of Hanoi with 8 rods?

how to solve the tower of hanoi with 8 rods in python. On the Internet, only 3 or 4, no more. Tell me the algorithm
It is necessary to calculate the minimum number of iterations in which all disks will move to spindle number 1 according to the following rules:
a) No more than one disk can be moved in one iteration
b) Disks can only be placed from a larger to a smaller one
c) You can shift from spindle number 8 disks only on spindles 7 and 6
d) From spindle number 1 it is possible to transfer disks only to spindles numbers 2 and 3
e) From spindles from 2 to 7 it is possible to transfer disks only to two adjacent spindles.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
W
Wataru, 2021-05-03
@dimaQd

Similar to the 4-spindle problem. To move the largest disk to the last spindle, you first need to put all the smaller disks somewhere. You can park them on the 2nd spindle or on the 8th. Then move the last disk to the 7th spindle, then re-park the part from the 8th to the 6th and move the last disk. Then back from 6th to 8th and the rest from 2nd to 8th. And you need to go through all the options - how many disks to shove.
As a result, write the following function: move n disks from the i-th spindle to the j-th one, with possibly prohibited spindles from the given mask. There you sort out where to put some of the upper disks and on which spindle. recursively count how many steps it will take, plus how many steps to move the remaining disks to the end, plus how many to move the top disks from the temporary spindle to the end. If there is only one disk left, then carefully look over whether it can be moved at all with the specified forbidden spindles (otherwise, lie + infinity, or something very large).
For this to work quickly, you need to do memoization and not count the function with the same set of parameters several times.
Interestingly, the answer grows much slower than for 3 spindles:

6
13
22
36
55
82
117
163
219
289
375
484
623
800
1024
1300
1651
2090
2643
3333

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question