# 雙色、三色河內塔

## 三色河內塔實作：ToyPythonProlog

``````def hanoi(n, a, b, c) {
if n == 1 {
println('{0} to {1}'.format(a, b))
println('{0} to {1}'.format(a, c))
println('{0} to {1}'.format(b, c))
}
else {
hanoi(n - 1, a, b, c)
println('{0} to {1}'.format(a, b))
hanoi(n - 1, c, a, b)
println('{0} to {1}'.format(a, c))
hanoi(n - 1, b, c, a)
println('{0} to {1}'.format(b, c))
hanoi(n - 1, a, b, c)
}
}

def two_colors_hanoi(n, a, b, c) {
if n == 1 {
println('{0} to {1}'.format(a, b))
println('{0} to {1}'.format(a, c))
}
else {
hanoi(n - 1, a, b, c)
println('{0} to {1}'.format(a, b))
hanoi(n - 1, c, a, b)
println('{0} to {1}'.format(a, c))
hanoi(n - 1, b, c, a)

two_colors_hanoi(n - 1, a, b, c)
}
}

two_colors_hanoi(3, 'A', 'B', 'C')
``````

• 雙色河內塔 Python
``````def hanoi(n, a, b, c):
if n == 1:
print('{} to {}'.format(a, b))
print('{} to {}'.format(a, c))
print('{} to {}'.format(b, c))
else:
hanoi(n - 1, a, b, c)
print('{} to {}'.format(a, b))
hanoi(n - 1, c, a, b)
print('{} to {}'.format(a, c))
hanoi(n - 1, b, c, a)
print('{} to {}'.format(b, c))
hanoi(n - 1, a, b, c)

def two_colors_hanoi(n, a, b, c):
if n == 1:
print('{} to {}'.format(a, b))
print('{} to {}'.format(a, c))
else:
hanoi(n - 1, a, b, c)
print('{} to {}'.format(a, b))
hanoi(n - 1, c, a, b)
print('{} to {}'.format(a, c))
hanoi(n - 1, b, c, a)

two_colors_hanoi(n - 1, a, b, c)

two_colors_hanoi(3, 'A', 'B', 'C')
``````

``````move(A, B) :- writef("%p to %p\n", [A, B]).

hanoi(1, A, B, C) :-
move(A, B), move(A, C), move(B, C), !.
hanoi(N, A, B, C) :-
M is N - 1,
hanoi(M, A, B, C),
move(A, B),
hanoi(M, C, A, B),
move(A, C),
hanoi(M, B, C, A),
move(B, C),
hanoi(M, A, B, C).

two_colors_hanoi(1, A, B, C) :-
move(A, B), move(A, C), !.

two_colors_hanoi(N, A, B, C) :-
M is N - 1,
hanoi(M, A, B, C),
move(A, B),
hanoi(M, C, A, B),
move(A, C),
hanoi(M, B, C, A).

main(_) :-
two_colors_hanoi(2, a, b, c).
``````

------------------------------------------------------------------------------------------------------------------

``````def hanoi(n, a, b, c) {
if n == 1 {
println('{0} to {1}'.format(a, b))
println('{0} to {1}'.format(a, b))
println('{0} to {1}'.format(a, c))
println('{0} to {1}'.format(b, c))
println('{0} to {1}'.format(b, c))
}
else {
hanoi(n - 1, a, b, c)
println('{0} to {1}'.format(a, b))
println('{0} to {1}'.format(a, b))
println('{0} to {1}'.format(a, b))

hanoi(n - 1, c, b, a)
println('{0} to {1}'.format(b, c))
println('{0} to {1}'.format(b, c))
println('{0} to {1}'.format(b, c))

hanoi(n - 1, a, b, c)
}
}

def three_colors_hanoi(n, a, b, c) {
if n == 1 {
println('{0} to {1}'.format(a, c))
println('{0} to {1}'.format(a, b))
println('{0} to {1}'.format(a, b))
println('{0} to {1}'.format(c, a))
println('{0} to {1}'.format(b, c))
}
else {
hanoi(n - 1, a, b, c)
println('{0} to {1}'.format(a, b))
println('{0} to {1}'.format(a, b))
println('{0} to {1}'.format(a, b))

hanoi(n - 1, c, b, a)
println('{0} to {1}'.format(b, c))
println('{0} to {1}'.format(b, c))
println('{0} to {1}'.format(b, c))

hanoi(n - 1, a, c, b)
println('{0} to {1}'.format(c, a))

hanoi(n - 1, b, c, a)
println('{0} to {1}'.format(c, b))

three_colors_hanoi(n - 1, a, b, c)
}
}

three_colors_hanoi(3, 'A', 'B', 'C')
``````

• 三色河內塔 Python
``````def hanoi(n, a, b, c):
if n == 1:
print('{} to {}'.format(a, b))
print('{} to {}'.format(a, b))
print('{} to {}'.format(a, c))
print('{} to {}'.format(b, c))
print('{} to {}'.format(b, c))
else:
hanoi(n - 1, a, b, c)
print('{} to {}'.format(a, b))
print('{} to {}'.format(a, b))
print('{} to {}'.format(a, b))

hanoi(n - 1, c, b, a)
print('{} to {}'.format(b, c))
print('{} to {}'.format(b, c))
print('{} to {}'.format(b, c))

hanoi(n - 1, a, b, c)

def three_colors_hanoi(n, a, b, c):
if n == 1:
print('{} to {}'.format(a, c))
print('{} to {}'.format(a, b))
print('{} to {}'.format(a, b))
print('{} to {}'.format(c, a))
print('{} to {}'.format(b, c))
else:
hanoi(n - 1, a, b, c)
print('{} to {}'.format(a, b))
print('{} to {}'.format(a, b))
print('{} to {}'.format(a, b))

hanoi(n - 1, c, b, a)
print('{} to {}'.format(b, c))
print('{} to {}'.format(b, c))
print('{} to {}'.format(b, c))

hanoi(n - 1, a, c, b)
print('{} to {}'.format(c, a))

hanoi(n - 1, b, c, a)
print('{} to {}'.format(c, b))

three_colors_hanoi(n - 1, a, b, c)

three_colors_hanoi(3, 'A', 'B', 'C')``````

``````move(A, B) :- writef("%p to %p\n", [A, B]).

hanoi(1, A, B, C) :-
move(A, B),
move(A, B),
move(A, C),
move(B, C),
move(B, C), !.
hanoi(N, A, B, C) :-
M is N - 1,
hanoi(M, A, B, C),
move(A, B), move(A, B), move(A, B),

hanoi(M, C, B, A),
move(B, C), move(B, C), move(B, C),

hanoi(M, A, B, C).

three_colors_hanoi(1, A, B, C) :-
move(A, C),
move(A, B),
move(A, B),
move(C, A),
move(B, C), !.

three_colors_hanoi(N, A, B, C) :-
M is N - 1,
hanoi(M, A, B, C),
move(A, B), move(A, B), move(A, B),

hanoi(M, C, B, A),
move(B, C), move(B, C), move(B, C),

hanoi(M, A, C, B),
move(C, A),

hanoi(M, B, C, A),
move(C, B),

three_colors_hanoi(M, A, B, C).

main(_) :-
three_colors_hanoi(2, a, b, c).
``````