My understanding of Hanoi

Source: Internet
Author: User

Recently in the review of C + + related algorithms, and this is the Chinese Connaught Tower, the Internet also read a lot of information, the code is very simple, but the sense of principle did not speak through. I was thinking about me to share my views on Hanoi.

First, Hanoi tower problem

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. The issue of Baidu Baidu Encyclopedia link: Click to open the link

Second, the code idea

Hanoi tower problem, simply means that there are a,b,c three pillars, and then move a plate to the C plate, but each move must ensure that the small plate on the big plate. B as an auxiliary plate.

First, when there are only 1 plates on a, move 1 times, move C from a.

When there are two plates on a, you must first move the small plate to B, then the big plate is moved from a to C. The small plate moves from B to C.

Analyzing two plates, two plates are a step closer to a plate, moving a small plate to B, and the other two are the essence of a move from a to C.

So what do we do when there are n plates on a? Whether our solution can be understood as having two plates in a state.

n Plates, a,b,c three pillars

The first step is to move the n-1 plate to B, i.e. F (n-1, a,c,b), which means N-1 's plate, moving through C for the middle, to the B-plate.

The second step, will be the bottom of the big plate, moved by A C Move (a,c)

The third step, at this time through the first two steps, a: 0 plates, b:n-1 a plate, C: 1 the largest plate

So the next step is to move B to C, where F (n-1,b,a,c) means N-1 's plate, moving through a to the middle, to the C drive.

In terms of recursion, there are two, the first one, the end of the loop, the second, the loop body.

So the end-of-cycle condition is, n<1, without plates.

#include <iostream>classhan{ Public:    intnum; Han (inta): num (a) {}voidMoveCharMCharN) {        Static intnumb =0; Numb++; Std::cout<<"Section"<< Numb <<"Times"<<Std::endl; Std::cout<< m <<" -"<< N <<Std::endl; }    voidPaiintNumCharACharBCharc) {        if(Num <1){            return; }        Else{pai (num-1, A, C, b);            Move (A,C); Pai (Num-1, B,a,c); }    }    voidsolve () {CharA ='A'; CharB ='B'; CharC ='C'; Pai ( This-num, A, B, C); }};intMain () {intn =0; Std::cout<<"Please enter the plate you want to put:"; Std::cin>>N;        Han Han1 (n);        Han1.solve (); Std::cout<<Std::endl; Std::cin.Get(); Std::cin.Get(); return 0;}

My understanding of Hanoi

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.