void towerOfHanoi(short height, short from, short to, short with) { // height = disks if (height > 0) { towerOfHanoi(height - 1, from, with, to); printf("Move the disk from %d to %d.\n", from, to); towerOfHanoi(height - 1, with, to, from); } }
void towerOfHanoi(int n, char fromrod, char torod, char auxrod) { if (n == 1) { printf("\n Move disk 1 from rod %c to rod %c", fromrod, torod); return; } towerOfHanoi(n-1, fromrod, auxrod, torod); printf("\n Move disk %d from rod %c to rod %c", n, fromrod, torod); towerOfHanoi(n-1, auxrod, torod, fromrod); }
void towerOfHanoi(std::stack<int> *a, std::stack<int> *buff, std::stack<int> *dest, int n) { if (n == 0) { return; } // Moving disks, using destination as a buffer towerOfHanoi(a, dest, buff, n-1); (*dest).push((*a).top()); (*a).pop(); // Moving from our buffer to the destination towerOfHanoi(buff, a, dest, n-1); }
int main() { std::stack<int> primary, buffer, destination; int n = 10; for (int i = n; i > 0; i--) { std::cout << i << " "; primary.push(i); } towerOfHanoi(&primary, &buffer, &destination, 10); std::cout << std::endl; while(!destination.empty()) { std::cout << destination.top() << " "; destination.pop(); } }
int main() { int n = 4; // Number of disks towerOfHanoi(n, 'A', 'C', 'B'); // A, B and C are names of rods return 0; }
int main() { int n = 4; towerOfHanoi(n, 'A', 'C', 'B'); return 0; }