//############################################################## // This is a solution to the tower of hanoi problem. //############################################################## #include #define MAX_DISKS 9 int Towers[3][MAX_DISKS]; int Ndisks[3]; // number of disks in each tower int Moves; void print_towers(); void toh (int n, int a, int b, int c); int main() { int n, i; do { printf("Enter number of disks (between 1 and %d): ", MAX_DISKS); scanf ("%d",&n); } while(n<1 || n > MAX_DISKS); for(i=0; i 0) { //1. move the top n-1 disks from a to c // Here we recursively call toh to do it. toh (n-1, a, c, b); //2. move the remaining disk from a to b printf ("Move a disk from %d to %d\n", a+1, b+1); Towers[b][Ndisks[b]] = Towers[a][Ndisks[a]-1]; Ndisks[a]--; Ndisks[b]++; print_towers(); Moves++; //3. move the n-1 disks from c to b toh (n-1, c, b, a); } } //============================================================== //============================================================== void print_towers() { int i, j; for(i=0; i<3; i++){ printf("[%d] ", i+1); for(j=0; j