Tower of Hanoi

VarunjeetsinghRekhi 339 views 11 slides Jun 15, 2021
Slide 1
Slide 1 of 11
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11

About This Presentation

Data Structures


Slide Content

It consists of three rods, and a number of disks of different sizes which can slide onto any rod. The puzzle starts with the disks in a neat stack in ascending order of size on one rod, the smallest at the top, thus making a conical shape.

RULES The mission is to move all the disks to some another tower without violating the sequence of arrangement. A few rules to be followed for Tower of Hanoi are − Only one disk can be moved among the towers at any given time. Only the "top" disk can be removed. No large disk can sit over a small disk. Following is an animated representation of solving a Tower of Hanoi puzzle with three disks.  Tower of Hanoi puzzle with n disks can be solved in minimum  2 n −1  steps. It is shows that a puzzle with 3 disks has taken  2 3  - 1 = 7  steps.

ALGORITHM If we have only one disk , then it can easily be moved from source to destination peg. If we have 2 disks − First, we move the smaller (top) disk to aux peg. Then , we move the larger (bottom) disk to destination peg. And finally, we move the smaller disk from aux to destination peg . To move n disks from source to destination, The steps to follow are - Step 1 − Move n-1 disks from source to aux Step 2 − Move n th disk from source to dest Step 3 − Move n-1 disks from aux to dest

A recursive algorithm for Tower of Hanoi can be driven as follows − START ProcedureHanoi (disk , source,dest , aux)  IF disk ==1, THEN move disk from source to dest ELSE Move(disk -1, source, dest , aux)// Step 1 Move(1 , source, aux,dest )// Step 2 move disk from source to dest Move(disk -1, aux, source, dest )// Step 3 END IF  ENDProcedure STOP

PROGRAM TO IMPLEMENT TOWER OF HANOI: #include < iostream.h > #include < conio.h >   void tower( int a, char source, char aux, char dest ) { if(a==1 ) { cout <<"\t\ tMove disc 1 from "<<source<<" to "<< dest <<"\ n"; return; } else{ tower(a-1,source,dest,aux); cout <<"\t\ tMove disc "<<a<<" from "<< souce <<" to "<< dest <<"\ n"; tower(a-1,aux,source,dest); } }

void main () { clrscr (); int n; cout <<"\n\t\t*****Tower of Hanoi*****\n"; cout <<"\t\ tEnter number of discs : "; cin >>n; cout <<"\n\n"; tower( n,'A','B','C '); getch (); }

OUTPUT

Thank You
Tags