0% found this document useful (0 votes)
1K views3 pages

Tower of Hanoi

The Tower of Hanoi is a mathematical puzzle that involves moving disks of different sizes stacked in descending order across three towers according to rules: only one disk can be moved at a time, only the top disk can be removed, and no disk can be placed on top of a smaller disk. The goal is to move all disks from the first tower to the last tower by following these rules. An algorithm uses recursion to solve the puzzle by breaking it down into moving n-1 disks, moving the largest disk, and then moving the remaining n-1 disks.

Uploaded by

PRABHAKAR MORE
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1K views3 pages

Tower of Hanoi

The Tower of Hanoi is a mathematical puzzle that involves moving disks of different sizes stacked in descending order across three towers according to rules: only one disk can be moved at a time, only the top disk can be removed, and no disk can be placed on top of a smaller disk. The goal is to move all disks from the first tower to the last tower by following these rules. An algorithm uses recursion to solve the puzzle by breaking it down into moving n-1 disks, moving the largest disk, and then moving the remaining n-1 disks.

Uploaded by

PRABHAKAR MORE
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Tower of Hanoi

Tower of Hanoi, is a mathematical puzzle which consists of three towers and more
than one rings is as depicted −

These rings are of different sizes and stacked upon in an ascending order, i.e. the
smaller one sits over the larger one. There are other variations of the puzzle where the
number of disks increase, but the tower count remains the same.
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.
Algorithm
If we have 2 disks −
o First, we move the smaller (top) disk to aux peg.
o Then, we move the larger (bottom) disk to destination peg.
o And finally, we move the smaller disk from aux to destination peg.

The steps to follow are −


Step 1 − Move n-1 disks from source to aux
Step 2 − Move nth 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
Procedure Hanoi(disk, source, dest, aux)

IF disk == 1, THEN
move disk from source to dest
ELSE
Hanoi(disk - 1, source, aux, dest) // Step 1
move disk from source to dest // Step 2
Hanoi(disk - 1, aux, dest, source) // Step 3
END IF

END Procedure
STOP
C Program for Tower of Hanoi
#include<stdio.h>
void TOH(int n,char x,char y,char z) {
if(n>0) {
TOH(n-1,x,z,y);
printf("\n%c to %c",x,y);
TOH(n-1,z,y,x);
}
}
int main() {
int n=3;
TOH(n,'A','B','C');
}
Output
A to B
A to C
B to C
A to B
C to A
C to B
A to B

You might also like