#include <iostream>
using namespace std;
//A recursive function that solve the puzzle of Towers of Hanoi
void towers_of_hanoi(int number_of_disks, string source_rod, string destination_rod, string auxiliary_rod)
{
if (number_of_disks == 1)
{
cout << "Movement of disk 1 from rod " << source_rod << " to rod " << destination_rod<<endl;
return;
}
towers_of_hanoi(number_of_disks - 1, source_rod, auxiliary_rod, destination_rod);
cout << "Move disk " << number_of_disks << " from rod " << source_rod <<
" to rod " << destination_rod << endl;
towers_of_hanoi(number_of_disks - 1, auxiliary_rod, destination_rod, source_rod);
}
// Testing code
int main()
{
int number_of_disks = 4; // Total number of disks
towers_of_hanoi(number_of_disks, "Rod A", "Rod C", "Rod B");
return 0;
}
Comments
Leave a comment