Created
October 23, 2024 20:02
-
-
Save juanfal/34a058da136fc6e9ad1efdb5eaafbed1 to your computer and use it in GitHub Desktop.
Hanoi towers recursion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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