Kerugaki-Blog Park http://www.cnblogs.com/kailugaji/
Hanoi is made up of three Poles a,b,c. A rod has n (n>1) perforated discs, the size of the disk from bottom to top in turn smaller. The following rules are required to move all discs to the C-bar: Only one disc can be moved at a time; the market cannot be stacked on a small plate. Tip: The disc can be temporarily placed in the B-bar, or the disc removed from the A will be re-moved back to the A-bar, but must respect the above two rules. Q: How do I move? How many times do you want to move at least?
Analysis:
(1) move a n-1 plate on a to B by using C;
(2) Move the remaining plate of a to C;
(3) Move the n-1 plate on B to c with the aid of a.
The program is implemented as follows:
1#include <stdio.h>2 3 intsum=0;//Global Variables4 5 voidMoveCharXChary) {6printf"%c-->%c\n", x, y);7sum=sum+1;8 }9 Ten intHanoiintNCharACharBCharc) { One if(n==1){ A Move (a,c); - } - the Else{ -Hanoi (n1, a,c,b); - Move (a,c); -Hanoi (n1, b,a,c); + } - } + voidMain () { A intm; at -printf"Please input a number:"); -scanf"%d",&m); - -printf"The step to moving%d disks:\n", m); -Hanoi (M,'A','B','C'); inprintf"It need%d steps\n", sum); -}
The result is:
C Language Hanoi Tower problem