Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Made tower of Hanoi clear #619

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions towerofhanoi.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
#include <iostream>
using namespace std;

void ToH(int n, char A, char B, char C)
void tower(int num, char src, char aux, char dest)
{
if (n == 1)
if (num == 1)
{
cout << "Move 1 from " << A << " to " << C << endl;
cout << "Move from " << src << " to " << dest << endl;
return;
}
ToH(n - 1, A, C, B);
cout << "Move " << n << " from " << A << " to " << C << endl;
ToH(n - 1, B, A, C);

tower(num - 1, src, dest, aux); //Visualize how if we could move n-1 discs
tower(1, src, aux, dest); //we would move them to aux, then one disc to dest
tower(num - 1, aux, src, dest); //then move the discs from aux to destination
}

int main()
Expand All @@ -19,7 +20,7 @@ int main()
int n;
cin >> n;

ToH(n, 'A', 'B', 'C');
tower(n, 'S', 'A', 'D');

return 0;
}