Tower of Hanoi is a mathematical puzzle where we have three rods and n disks. The objective of the puzzle is to move all disks from the source rod to the destination rod using a third rod (say auxiliary). The rules are :

Tower of Hanoi

PROBLEM:-Tower of Hanoi is a mathematical puzzle where we have three rods and n disks. The objective of the puzzle is to move all disks from source rod to destination rod using third rod (say auxiliary). The rules are :

1) Only one disk can be moved at a time.
2) A disk can be moved only if it is on the top of a rod.
3) No disk can be placed on the top of a smaller disk.

Source Rod is named as ‘a’, auxiliary rod as ‘b’ and destination rod as ‘c’.

Input Format :
Integer n
Output Format :
Steps in different lines (in one line print source and destination rod name separated by space)
Constraints :
0 <= n <= 20
Sample Input 1 :
2
Sample Output 1 :
a b
a c
b c
Sample Input 2 :
3
Sample Output 2 :
a c
a b
c b
a c
b a
b c
a c

SOLUTION

#include <iostream>
using namespace std;

void towerOfHanoi(int n, char source, char auxiliary, char destination) {
    // Write your code here
    if(n==0)
        return;
    if(n==1)
        cout<<source<<' '<<destination<<endl;
    else if(n==2)
    {
        
        cout<<source<<' '<<auxiliary<<endl;
        cout<<source<<' '<<destination<<endl;
        cout<<auxiliary<<' '<<destination<<endl;
        
    }
    else{
        towerOfHanoi(n-1,source,destination,auxiliary);
        cout<<source<<' '<<destination<<endl;
        towerOfHanoi(n-1,auxiliary,source,destination);
    }

}


int main() {
    int n;
    cin >> n;
    towerOfHanoi(n, 'a', 'b', 'c');
}

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *