def hanoi(n,x,y,z): if n == 1: print(x,'-->',c) else: hanoi(n-1,x,z,y)
# Before n- A plate from x Move to y upper hanoi(1,x,y,z) # Will be the first n A plate from x Move to z upper hanoi(n-1,y,x,z)
# take y Upper n-1 A plate from y Move to z upper n = int(input(' Please enter the number of floors of Hanoi Tower :')) hanoi = (n,'x','y','z')
study Python Practical difficulties encountered in , Hanoi Tower program , I hope it will be helpful to you ,
The details are as follows :
It was defined when I first encountered difficulties hanoi() During function call ,x,y,z I can't understand the order of , In fact, another idea ,
No matter how you move , Can be divided into three columns , Starting column , Auxiliary column , Target column
First step : take n-1 A plate from x Move to y The reality is :x Is the starting column ,z Auxiliary column ,y Target column
Step two : Will be the first n A plate from x Move to z The reality is :x Is the starting column ,y Auxiliary column ,z Target column
Step 3 : Will be in y Column n-1 A plate from y Move to z The reality is :y Is the starting column ,x Auxiliary column ,z Target column
Therefore, the call result is :
haooi (n-1,x,z,y)
hanoi (1,x,y,z)
hanoi (n-1,y,x,z)
I hope it will be helpful to you !!! Get more benefits below !
Technology
Daily Recommendation