Three pegs A, B, C; A holds n disks, big ones at the bottom. Move one disk at a time, and never put a big disk on a small one. Finish hanoi(n, from, via, to): using via, move n disks from from to to, showing each step as A->C. The program moves 3 disks and then shows the total number of steps.
A->C A->B C->B A->C B->A B->C A->C 7
#include <stdio.h>
int moves = 0;
void hanoi(int n, char from, char via, char to) {
if (n == 1) {
printf("%c->%c\n", from, to);
moves++;
return;
}
// your code here: first move the top n-1 disks from from to via
printf("%c->%c\n", from, to);
moves++;
// your code here: then move those n-1 disks from via to to
}
int main(void) {
hanoi(3, 'A', 'B', 'C');
printf("%d\n", moves);
return 0;
}
Run tries the examples. When they look right, Submit.