C Language Hanoi Tower problem

Source: Internet
Author: User

Kerugaki-Blog Park http://www.cnblogs.com/kailugaji/

Hanoi is made up of three Poles a,b,c. A rod has n (n>1) perforated discs, the size of the disk from bottom to top in turn smaller. The following rules are required to move all discs to the C-bar: Only one disc can be moved at a time; the market cannot be stacked on a small plate. Tip: The disc can be temporarily placed in the B-bar, or the disc removed from the A will be re-moved back to the A-bar, but must respect the above two rules. Q: How do I move? How many times do you want to move at least?

Analysis:

(1) move a n-1 plate on a to B by using C;

(2) Move the remaining plate of a to C;

(3) Move the n-1 plate on B to c with the aid of a.

The program is implemented as follows:

1#include <stdio.h>2 3 intsum=0;//Global Variables4 5 voidMoveCharXChary) {6printf"%c-->%c\n", x, y);7sum=sum+1;8 }9 Ten intHanoiintNCharACharBCharc) { One     if(n==1){ A Move (a,c); -     } -  the     Else{ -Hanoi (n1, a,c,b); - Move (a,c); -Hanoi (n1, b,a,c); +     } - } + voidMain () { A     intm; at  -printf"Please input a number:"); -scanf"%d",&m); -  -printf"The step to moving%d disks:\n", m); -Hanoi (M,'A','B','C'); inprintf"It need%d steps\n", sum); -}

The result is:

C Language Hanoi Tower problem

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.