20150410 recursive implementation of Hanoi algorithm

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.