Untitled
unknown
plain_text
8 months ago
420 B
4
Indexable
int toh(int n, int src, int helper, int dest) {
if (n == 1) {
return 1; // Base case: Only one move required for a single disk.
}
int count = 0;
count += toh(n - 1, src, dest, helper); // Move `n-1` disks from src to helper.
count += 1; // Move the nth disk from src to dest.
count += toh(n - 1, helper, src, dest); // Move `n-1` disks from helper to dest.
return count;
}
Editor is loading...
Leave a Comment