|
Tower of Henoi |
It is a mathematical puzzle in which three towers and more than one different sizes rings are stacked in ascending order. In this puzzle number of disc can be increases. Disc are kept in this way that smaller disc sits over bigger disc.
The task of this puzzle is to move the discs to another tower without breaking the sequence of arrangement.
1. Only one disc can be move at a time.
2. Only upper disc can be move.
3. Bigger disc can not be sit over small disc.
def towerOfHenoi(n, source, to,intermediate):
if(n==1):
print("Transfer disc", source ,"to", to)
else:
towerOfHenoi(n-1,source,intermediate,to)
print("Transfer disc", source ,"to", to)
towerOfHenoi(n-1,intermediate,to,source,)
numOfdisc=int(input("Enter number of disc"))
towerOfHenoi(numOfdisc,'A','C','B')
Enter number of disc 3
Transfer disc A to C
Transfer disc A to B
Transfer disc C to B
Transfer disc A to C
Transfer disc B to A
Transfer disc B to C
Transfer disc A to C
Comments
Post a Comment