Tower of Hanoi

So, welcome to day 13 of my 100-day DSA Challenge. Today, while looking up heap sort in particular and some more recursion problems, I came across one mathematical problem solved with the help of coding i.e. Tower of Hanoi. I hope everyone remembers this concept from their junior classes.

The Tower of Hanoi is a way of arranging the levels of the tower, with decreasing order of their width when seen from the top of the tower. However, some limitations bounding this mathematical concept and those are:-

  1. We can only access one disk or rather one level at one time.

  2. The smaller disk must not be placed above the larger disk.

While keeping these two rules in mind, we've to develop an algorithm that can solve this mathematical problem.

#include <bits/stdc++.h>
using namespace std;

class Solution{
    public:
    void toh(int N, int from, int to, int aux) {
        // Your code here
        if(N == 0){
            return ;
        }
        toh(N-1, from, aux, to);
        cout<<"move disk " << N << " from rod " << from << " to rod " << to  << endl;
        toh(N-1, aux, to, from);
    }

};

int main() {

    int T;
    cin >> T;
    while (T--) {

        int N;
        cin >> N;
        Solution ob;

        cout << ob.toh(N, 1, 3, 2) << endl;
    }
    return 0;
}