## Towers of HanoiA "Divide-and-Conquer" Algorithm
The Towers of Hanoi is a classic problem where you try to move all of the discs
on one peg to another peg using only three pegs. Initially, all of the
discs are stacked on top of each other with the larger discs under the smaller discs.
You may move a disc to any of the three pegs as you attempt to relocate all of the discs,
but you can This problem can be easily solved with a recursive "divide-and-conquer" algorithm. Here's how it works:
Assume you have The program for solving this problem would be: void main() { int n = 4; /* Number of discs = 4 */ hanoi('A','B','C', n); } void hanoi(char From, char To, char Other, int n) { if (n == 0) return; hanoi(From, Other, To, n-1); printf("Moving disc from peg %c to %c\n", From, To); hanoi(Other, To, From, n-1); } The output of the program (which moves 4 discs) would be: Moving disc from peg A to C Moving disc from peg A to B Moving disc from peg C to B Moving disc from peg A to C Moving disc from peg B to A Moving disc from peg B to C Moving disc from peg A to C Moving disc from peg A to B Moving disc from peg C to B Moving disc from peg C to A Moving disc from peg B to A Moving disc from peg C to B Moving disc from peg A to C Moving disc from peg A to B Moving disc from peg C to B |