Skip to content

Instantly share code, notes, and snippets.

@juanfal
Created October 23, 2024 20:02
Show Gist options
  • Save juanfal/34a058da136fc6e9ad1efdb5eaafbed1 to your computer and use it in GitHub Desktop.
Save juanfal/34a058da136fc6e9ad1efdb5eaafbed1 to your computer and use it in GitHub Desktop.
Hanoi towers recursion
// hanoi.cpp
// juanfc 2024-10-23
//
#include <iostream>
using namespace std;
// prototypes
int main()
{
void moveGroupUsing(int n, char base, char inter, char dest);
// int noDiscs;
// do {
// cout << "¿No of discs (0 end): ";
// cin >> noDiscs;
// moveGroupUsing(noDiscs, 1, 2, 3);
// } while (noDiscs > 0);
moveGroupUsing(3, 'B', 'i', 'D');
return 0;
}
void moveGroupUsing(int n, char base, char inter, char dest)
{
if ( n > 0 ) {
moveGroupUsing( n-1, base, dest, inter );
cout << '\t' << base << " -> " << dest << endl;
moveGroupUsing( n-1, inter, base, dest );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment