Tower of Hanoi
Introduction:
Tower of Hanoi is a
mathematical puzzle where we have three rods (A, B, and C) and N disks.
Initially, all the disks are stacked in decreasing value of diameter i.e., the
smallest disk is placed on the top and they are on rod A. The objective of
the puzzle is to move the entire stack to another rod (here considered C),
obeying the following simple rules:
·
Only one disk can be moved at a time.
·
Each move consists of taking the upper
disk from one of the stacks and placing it on top of another stack i.e. a disk
can only be moved if it is the uppermost disk on a stack.
·
No disk may be placed on top of a smaller
disk.
Algorithm:
Follow the steps
below to solve the problem:
·
Create
a function towerOfHanoi where pass the N (current number of
disk), from_rod, to_rod, aux_rod.
·
Make
a function call for N – 1 th disk.
·
Then
print the current the disk along with from_rod and to_rod
·
Again
make a function call for N – 1 th disk.
Code:
// C++ recursive
function to
// solve tower of
hanoi puzzle
#include
<bits/stdc++.h>
using namespace
std;
void
towerOfHanoi(int n, char from_rod, char to_rod,
char
aux_rod)
{
if (n == 0) {
return;
}
towerOfHanoi(n - 1, from_rod,
aux_rod, to_rod);
cout << "Move disk "
<< n << " from rod " << from_rod
<< " to rod
" << to_rod << endl;
towerOfHanoi(n - 1, aux_rod, to_rod,
from_rod);
}
// Driver code
int main()
{
int N = 3;
// A, B and C are names of rods
towerOfHanoi(N, 'A', 'C', 'B');
return 0;
}
ABOUT THE AUTHOR
Hello We are OddThemes, Our name came from the fact that we are UNIQUE. We specialize in designing premium looking fully customizable highly responsive blogger templates. We at OddThemes do carry a philosophy that: Nothing Is Impossible
.png)
0 comments:
Post a Comment