First, go to the code
1 DefHanoi_move (n, source, DEST, intermediate ):2IfN> = 1:#Recursive exit, with only one plate left3Hanoi_move (n-1, Source, intermediate, DEST)4Print("Move % s-> % s"%(Source, DEST ))5Hanoi_move (n-1, intermediate, DEST, source)
First, three poles are discharged in sequence, namely source, DEST, and intermediate for the source rod, media rod, and target rod Code. There are n big cakes on the source rod.
We define a function def Hanoi (n, source rod, target rod, and media rod): # It means that the source rod uses the media rod to the target rod.
We assume n-1 layers above the bottom layer have been placed, that is, there are currently only two big cakes on the source rod. The operation we want to perform is:
The n-1 layer on the source rod is equivalent to the n-1 layer on the media Rod Using the target rod to the media rod.
Print the path from the source pole to the target pole
Then, the n-1 layer of the media rod is equivalent to Hanoi (n-1, media rod, target rod, and source rod) by using the source rod to the target rod)
Let's talk about the assumption of N-1 layer, because we used recursion when assuming n-1 layer, in Hanoi (n-1 ...) We assume that the N-2 layer has been placed, and then in Hanoi (n-2 ...) When we assume that the n-3 layer has been placed, continuous backtracking, to the top layer has been placed, this assumption is reasonable, you can use this method. With Recursive Backtracking, it is basically impossible to use recursive ideas to continuously introduce his steps. However, it is easier to get results by making assumptions on the premise.
The quanta problem is actually very simple.