Created
October 30, 2013 01:13
-
-
Save iToto/7225647 to your computer and use it in GitHub Desktop.
Hanoi4
This file contains hidden or 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
#include <iostream.h> | |
void restack(char source,char dest,char temp) | |
{ | |
cout << source << " --> " << temp << endl; | |
cout << source << " --> " << dest << endl; | |
cout << temp << " --> " << dest << endl; | |
} | |
void Hanoi4(int nDisks, char source, char intermed1, char intermed2, char dest) | |
{ | |
if ( nDisks == 1 ) | |
cout << source << " --> " << dest << endl; | |
else if ( nDisks == 2 ) | |
{ | |
// re-stack two disks to destination | |
restack(source,dest,intermed1); | |
} | |
else | |
{ | |
Hanoi4(nDisks - 2, source, intermed2, dest, intermed1); | |
restack(source,dest,intermed2); | |
Hanoi4(nDisks - 2, intermed1, source, intermed2, dest); | |
} | |
} | |
int main() | |
{ | |
Hanoi4(4, 'A', 'B', 'C', 'D'); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
you can be really Hanoying sometimes...