Tower of Henoi Algorithm.

Tower of Hanoi:


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.

Rules to Play:

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.

Source code:

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')

output:

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

Popular posts from this blog

Introduction to Python.

Decision Making Statement.