Skip to content

Instantly share code, notes, and snippets.

@iToto
Created October 30, 2013 01:13
Show Gist options
  • Save iToto/7225647 to your computer and use it in GitHub Desktop.
Save iToto/7225647 to your computer and use it in GitHub Desktop.
Hanoi4
#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;
}
@grahamsaulnier
Copy link

you can be really Hanoying sometimes...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment