From 088962181e6a13a5125f8a1a3b59e4f094d10b26 Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 21 Apr 2023 07:25:28 +0530 Subject: [PATCH] Made tower of Hanoi clear --- towerofhanoi.cpp | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/towerofhanoi.cpp b/towerofhanoi.cpp index dffb0831..92a6d090 100644 --- a/towerofhanoi.cpp +++ b/towerofhanoi.cpp @@ -1,16 +1,17 @@ #include 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() @@ -19,7 +20,7 @@ int main() int n; cin >> n; - ToH(n, 'A', 'B', 'C'); + tower(n, 'S', 'A', 'D'); return 0; }