Hanoi Tower Problem

Source: Internet
Author: User

Suppose there are a, B, c three axes, there are n of different diameters, from small to large sequentially numbered 1,2,3......,n of the disk in the order of the upper and lower in a large number stacked on a. It is now required to move the n discs to the C axis and still stack them in the same order. However, the following rules must be followed when the disc is stacked: 1. Only one disc can be moved at a time, it must be at the top of an axis; 2. The disc may be inserted on any axis in a, B, C, 3. The smaller discs cannot be placed on larger discs at any time.

Code:

#include <stdio.h>

Move (char A,char B)
{
printf ("%c-->%c\n", A, b);
}

Hanio (int n,char a,char B,char C)
{
if (n==1)
Move (A,C);
Else
{
Hanio (N-1,A,C,B);
Move (A,C);
Hanio (N-1,B,A,C);
}
}

int main ()
{
int n;
scanf ("%d", &n);
Hanio (n, ' A ', ' B ', ' C ');
return 0;
}

Hanoi Tower Problem

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.