def move(n, a, b, c):
print n,a,b,c
if n ==1:
print a, '-->', c
return
move(n-1, a, c, b)
print 'one',n-1,a,c,b
move(1,a,b,c)
print 'two',1,a,b,c
move(n-1, b, a, c)
print 'three',n-1,b,a,c
move(4, 'A', 'B', 'C')
运行结果:
4 A B C
3 A C B
2 A B C
1 A C B
A --> B
one 1 A C B
1 A B C
A --> C
two 1 A B C
1 B A C
B --> C
three 1 B A C
one 2 A B C
1 A C B
A --> B
two 1 A C B
2 C A B
1 C B A
C --> A
one 1 C B A
1 C A B
C --> B
two 1 C A B
1 A C B
A --> B
three 1 A C B
three 2 C A B
one 3 A C B
1 A B C
A --> C
two 1 A B C
3 B A C
2 B C A
1 B A C
B --> C
one 1 B A C
1 B C A
B --> A
two 1 B C A
1 C B A
C --> A
three 1 C B A
one 2 B C A
1 B A C
B --> C
two 1 B A C
2 A B C
1 A C B
A --> B
one 1 A C B
1 A B C
A --> C
two 1 A B C
1 B A C
B --> C
three 1 B A C
three 2 A B C
three 3 B A C