Hanoi Hanoi Tower Problem

Source: Internet
Author: User

Title Description Description

There are n disks, according to the radius of the size (the radius is different), the bottom-up on a column, each time only allowed to move the top of a plate to the other column up (except a column, there are B and C columns, the beginning of the two pillars on the plate), but never allow the appearance of large plates on the pillar, the small It is now required to design a method for moving n plates on a pillar to the C column.

input/output format input/output Input Format:
One line, n<=20
output Format:Step number and the steps of the various discs to be moved input and Output sample sample Input/output sample Test point # # Input Sample:2 Sample output: 1:from A to B 2:from A to C 3:from B to C sample Test Point # # Input Sample:3 Sample output: 1:from A to C 2:from A to B 3:from C to B 4:from A to C 5:from B to A 6:from B to C 7:from A to C Ideas:

The topic is a typical recursive programming problem.

(1) When n=1, only one plate, only need to move once:a->c;

(2) When n=2, you need to move three times:

A------1------> B, a------2------> C, B------1------> C.

(3) If n=3, then the specific move steps are:

Assuming that the 3rd, 4th, and 7th steps are taken out to be equivalent to the situation of the n=2 (2 pieces are bundled together as one piece):

So you can press the "n=2" Move Step design:

① if the n=0, then exit, that is, the end of the program, otherwise continue to execute;

② with the C-pillar as the assistance transition, the (N-1) piece on the A column is moved to the B-pillar, the process mov (n-1, a,b,c) is called;

③ the remaining piece of a column directly onto the C-pillar;

④ with a column as an aid transition, the B-pillar (N-1) is moved to the C-pillar, calling process mov (N-1,B,C,A)

The code is as follows:

1#include <stdio.h>2 intk=0;3 voidHanoi (intNCharACharBCharc)4 {5k++;6         if(n==0)//0 words, no fun, direct exit!! 7     {8         return ; 9     }Ten     Else One     { AHanoi (n1, a,c,b); -printf"%d:from%c to%c\n"k,a,c); -Hanoi (n1, b,a,c); the     } - } - intMain () - { +     intN; -scanf"%d",&n); +Hanoi (N,'A','B','C');  A     return 0; at}

In order to help students better understand the problem of recursive algorithm, you can go to download a ppt, this very detailed: http://yunpan.cn/cjjutfERXMbXm access password: 6712

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