C Recursive Implementation Hanoi

Source: Internet
Author: User

1 Hanoi Description:

Hanoi (also known as Hanoi) is a puzzle toy derived from an ancient Indian legend. When big Brahma created the world, he made three diamond pillars, and stacked 64 gold discs on a pillar from bottom to top in order of size. The great Brahma commanded the Brahman to rearrange the discs from below to the other pillars in order of size. It is also stipulated that the disc cannot be enlarged on the small disc, and only one disc can be moved between the three pillars at a time.


2 Chinese nuo pseudo algorithm:

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 help of other towers, the rest is the same (only the complexity of the event)


3 Assuming that the plate is 3 times, the figure depicts



4 C language complete code

#include <stdio.h>

int num = 0;//counter, calculate how many steps
char a = ' a ', B = ' B ', c = ' C ';//analog a,b,c three towers//
 Getother The function is to get another tower name, assuming that from
 = A, to = B, then return to C
*
/char getother (char From,char to) {
  if (a! = from && a!). = to) {
    return A;
  } else if (B! = from && b! = to) {
    return b;
  } else{
    return C;
  }
}

Used to output from which tower to which tower
Void Move (char from, char to) {
  num++;
  printf ("Step%d:%c-to-%c\n", num,from,to);
};

Implement function
void Hanoi (int n, char From,char to) {
  if (n==1) {
    move (from,to);
  } else{
      Char other =  getother (from,to);
      Hanoi (n-1,from,other);
      Move (from,to);
      Hanoi (n-1,other,to);
  }
}

int main () {

int n = 2;
printf ("There are%d plates when: \ n");
Hanoi (n, ' A ', ' C ');
return 1;
};

5 output result diagram

5.1 Assume there are 2 plates.




5.2 Off with 3 plates



5.3 Fake with 64 plates, the computer ran for half an hour, have not finished calculating ... Sweat... Garbage configuration, it is not mapped.










Related Article

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.