Write a move(n, a, b, c) function that receives parameters that n represent the number of plates of the 1th pillar A in the 3 pillars A, B, C, and then prints out a way to move all the plates from a to C with a B, for example:
def move (n, a, B, c):
Pass
Answer:
def move(n,a,b,c): if n= =1: Print(a,'-a ',c) < span class= "n" > < Span class= "p" > else: < span class= "n" > < Span class= "p" > move (n-1,a ,c,b) < Span class= "o" > < span class= "n" > move (1,a,b ,c) < Span class= "p" > < span class= "n" > move ( N-1,b,a ,c) The key to understanding does not need to be how each step is solved. The point is to achieve your goal. We can understand this:
Move (n, start, buffer, end) Our step is to move the top n-1 plate to the buffer Move (N-1,A,C,B) and move the largest plate to the finish line.< Span class= "o" > < span class= "n" > move (1,a,b ,c) 。 The last buffer becomes the starting point and the starting point becomes the buffer.< Span class= "p" > < span class= "n" > move ( N-1,b,a ,c)
A detailed recursive algorithm for Hanoi in Python