Hanoi Tower Problem

Source: Internet
Author: User

Hanoi: Hanoi (also known as Hanoi) is a puzzle toy 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.

There are three adjacent pillars, labeled a,b,c,a pillars from bottom to top by pyramid-shaped stack of n different size of the disc, to move all the plate one to the pillar B, and each move on the same pillar can not appear on the big plate on the small plate above, ask at least how many times to move, set the number of moves to H ( n), and calculate the way to move

This is a typical recursive problem, we can use one of the pillars to assist the movement, the rule is: a initial n plate, moving the target is C, we can now move the N-1 with C to B, and then move the nth plate to C, and finally the B N-1 plate with a moved to C


#include <stdio.h>void hannuota (int disk_count, char pillar_1,char Pillar_2,char pillar_3) {if (disk_count==1) printf ("\ n moves the plate numbered%d from the%c pillar to the%c pillar", disk_count,pillar_1,pillar_3); Else{hannuota (Disk_count-1,pillar_1,pillar_3, pillar_2);p rintf ("\ n move a plate numbered%d from the%c pillar to the%c pillar", disk_count,pillar_1,pillar_3); Hannuota (disk_count-1,pillar_2,pillar _1,pillar_3);}} int main (int argc, char** argv) {int N;char pillar_1= ' A ', pillar_2= ' B ', pillar_3= ' C '; scanf ("%d", &n); Hannuota (N, pillar_1,pillar_2,pillar_3); 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.