20150410 recursive implementation of Hanoi algorithm
2015-04-10 Lover Snow Child
1 //Hanoi2#include <stdio.h>3 4 Static inti =0;5 6 //move n plates from X to Z with y7 //N: Number of moves x: Source address y: Middle pillar Z: Destination Pillar8 voidMoveintNCharXCharYCharz)9 {Ten if(1==N) { Oneprintf"page%d mobile%c--->%c\n", ++I, x, z); A}Else{ -Move (n1, X, z, y);//N-1 a plate from X with Z to y -printf"page%d mobile%c--->%c\n", ++i, x, z);//Move the nth plate from X to Z theMove (n1, y, x, z);//move n-1 plates from Y to Z with x - } - } - + intMainvoid){ - intN; + Aprintf"Please enter the number of layers for Hanoi: \ n"); atscanf"%d",&n); -printf"the steps to move are as follows: \ n"); -Move (N,'X','Y','Z'); -printf"The move was complete, and the total went%d times!\n", i); - return 0; -}
When the Hanoi layer is 3 o'clock, the total number of walks is 7 times:
When the number of layers of Hanoi is 4 o'clock, a total of 15 has gone:
20150410 recursive implementation of Hanoi algorithm