Skip to content
This repository was archived by the owner on Oct 10, 2022. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions Tower_of_Hanoi.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@

// in this approach we use recursion and take the help of the auxilary rod to move all the discs from
//one rod to another
// our refrenced auxilary rod changes everytime and we call the function recursively

// Time complexity: O(2^N)
//Auxiliary Space: O(N)
//code
#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);
}


int main()
{
int N = 3;


towerOfHanoi(N, 'A', 'C', 'B');
return 0;
}