Menu
- 1.Write a Program to Implement Breadth First Search using Python.
- 2.Write a Program to Implement Depth First Search using Python
- 3.Write a Program to Implement Tic-Tac-Toe game using Python.
- 4.Write a Program to Implement 8-Puzzle problem using Python.
- 5.Write a Program to Implement Water-Jug problem using Python.
- 6.Write a Program to Implement Travelling Salesman Problem using Python.
Aim: Write a Program to Implement Tower of Hanoi using Python.
Program:
# Recursive Python function to solve the tower of hanoi
def TowerOfHanoi(n, source, destination, auxiliary):
if n == 1:
print("Move disk 1 from source", source, "to destination", destination)
return
TowerOfHanoi(n-1, source, auxiliary, destination)
print("Move disk", n, "from source", source, "to destination", destination)
TowerOfHanoi(n-1, auxiliary, destination, source)
# Driver code
n = 4
TowerOfHanoi(n, 'A', 'B', 'C')
# A, C, B are the name of rods
Output:
Move disk 1 from rod A to rod B
Move disk 2 from rod A to rod C
Move disk 1 from rod B to rod C
Move disk 3 from rod A to rod B
Move disk 1 from rod C to rod A
Move disk 2 from rod C to rod B
Move disk 1 from rod A to rod B
Move disk 4 from rod A to rod C
Move disk 1 from rod B to rod C
Move disk 2 from rod B to rod A
Move disk 1 from rod C to rod A
Move disk 3 from rod B to rod C
Move disk 1 from rod A to rod B
Move disk 2 from rod A to rod C
Move disk 1 from rod B to rod C