def move(n, a, b, c):
if n == 1:
print('move', a, '-->', c)
else:
move(n-1, a, c, b)#当大于1个的时候先把a移到b (小的那个移到b当中介)
move(1, a, b, c)#把a移到b后,然后把a移到c(大的那个移到c当底子)
move(n-1, b, a, c)#把a的两个都移走后,把小的那个(b)上的那个移回c上(把两个中小的b移到c上)
move(3, 'A', 'B', 'C')
Sign in to make a reply
岁羽谌谌
def move(n, a, b, c):
if n == 1:
print('move', a, '-->', c)
else:
move(n-1, a, c, b)#当大于1个的时候先把a移到b (小的那个移到b当中介)
move(1, a, b, c)#把a移到b后,然后把a移到c(大的那个移到c当底子)
move(n-1, b, a, c)#把a的两个都移走后,把小的那个(b)上的那个移回c上(把两个中小的b移到c上)
move(3, 'A', 'B', 'C')