For example if I use 4 disks, The problem should be solved in 3 steps according to the equation. However, mine takes 9 steps. Why is it so?
Consider the code here
include <stdio.h>
void towerOfHanoi(int n, char from_rod, char to_rod,
char aux_rod1, char aux_rod2)
{
if (n == 0)
return;
if (n == 1) {
printf("\n Move disk %d from rod %c to rod %c",
n, from_rod, to_rod);
return;
}
towerOfHanoi(n - 2, from_rod, aux_rod1, aux_rod2,
to_rod);
printf("\n Move disk %d from rod %c to rod %c ",
n - 1, from_rod, aux_rod2);
printf("\n Move disk %d from rod %c to rod %c ",
n, from_rod, to_rod);
printf("\n Move disk %d from rod %c to rod %c ",
n - 1, aux_rod2, to_rod);
towerOfHanoi(n - 2, aux_rod1, to_rod, from_rod,
aux_rod2);
}
// driver program
int main()
{
int n = 4; // Number of disks
// A, B, C and D are names of rods
towerOfHanoi(n, 'A', 'D', 'B', 'C');
return 0;
}