Hanoi using recursive algorithm

Source: Internet
Author: User

Hanoi Principle Analysis:

When there is only one plate, you need to move from a tray on tower A to Tower C.

When there are two plates on tower A, move the No. 1th plate on Tower A (numbered from top to bottom) to Tower B, then move the No. 2nd plate on Tower C and move the small plate on Tower B to Tower C.

When there are 3 plates on tower A, move the plates numbered 1 to 2 on tower A to Tower B (with the help of Tower C), move the largest plate on tower A to tower C, and finally move the 3rd plates on Tower B to Tower C with the help of Tower A.

When there are n plates on tower A, move the plate numbered 1 to n-1 (n-1) on Tower A to Tower B (with tower C), then move the largest n plate on tower A to Tower C, and then move the n-1 tray on Tower B to Tower C with a tower.

To sum up, except that there is only one plate without the use of other towers, the rest is the same.

[CPP]View Plaincopy
  1. <span style= "FONT-SIZE:14PX;" > #include <iostream>
  2. using   namespace &NBSP;STD;&NBSP;&NBSP;
  3. int  hannuota ( int  n,string a,string b,string c)   
  4. {&NBSP;&NBSP;
  5.     if (n==1)   
  6. &NBSP;&NBSP;&NBSP;&NBSP;{&NBSP;&NBSP;
  7.         //with only one plate, move the plate on the first tower directly to the third tower &NBSP;&NBSP;
  8.         printf (" tower%s------> Tower%s\n " ,a.c_str (), C.c_str ());   
  9. &NBSP;&NBSP;&NBSP;&NBSP;}&NBSP;&NBSP;
  10.     else {   
  11.         //1. First the n-1 plate of the first tower is moved through the third tower to the second tower. &NBSP;&NBSP;
  12.         hannuota (n-1,a, c, b);   
  13.         //2. Move the remaining plate to the third tower. &NBSP;&NBSP;
  14.         printf (" tower%s------> Tower%s\n " ,a.c_str (), C.c_str ());   
  15.         //3. Finally, the plate on the second tower is moved through the first tower to the third tower. &NBSP;&NBSP;
  16. Hannuota (n-1, B, A, c);
  17. }
  18.     return  1;  
  19. }
  20. intMain (intargc,Const Char* argv[]) {  
  21.     printf (" Please enter the number of plates: \ n "
  22.     int  n;  
  23.     scanf ("%d " ,&n);   
  24.     printf (" the plate moves as follows: \ n "
  25.     hannuota (N," A " " B " " C "
  26.     return  0;  
  27. }</span>

Hanoi using recursive 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.