[Python] Tower of Hanoi problem

count = 0 
def hannuota(n,src,dst,mid): #n is the number of discs, src is the start, dst is the target, mid is the excess
global count
if n == 1:
print('{}:{} ->{}'.format(1,src,dst)) #When the disc is 1, move from the starting column to the target column
count += 1
else:
hannuota(n-1,src,mid,dst) # The remaining discs are moved from A to B column
print('{}:{}->{}'.format(n,src,dst)) #The largest disc is moved from A to C column
count += 1  
hannuota( n-1,mid,dst,src) #The remaining discs go from B pillar to C pillar
hannuota(3,'A','C','B')
print(count) #What

I understand now is -> imagine There are 1, 2, 3, 4, 5 discs, first move the top four discs from A to B post, then move 5 from A to C post, then move 1234 from B to C post, then move These actions are decomposed into n=1.
#I don't understand either, it's so hard!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325064737&siteId=291194637