A graphical recursive algorithm for Hanoi

Source: Internet
Author: User

A Origin:

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

Two Abstract as a mathematical problem:

As shown, from left to right there are a, B, C three pillars, where a column above the small stack to the large n disk, is required to move a pillar on the C pillar up, there is only one principle: only one plate can be moved at a time and large plates can not be on the small plate, to move the steps and the number of moves

Solution: (1) n = = 1

1th time 1th >c A---- sum = 1 times

(2) n = = 2

1th time 1th >b A----

2nd time 2nd >c A----

3rd time 1th B---->c sum = 3 times

(3) n = = 3

1th time 1th >c A----

2nd time 2nd >b A----

3rd time 1th >b C----

4th time 3rd >c A----

5th time 1th >a B----

6th time 2nd >c B----

7th time 1th >c A----sum = 7 times

It is not difficult to find the rule: the number of a disk 2 of the 1 square minus 1

2 discs 2 of 2 square minus 1

3 discs 2 of 3 square minus 1

。 。 。 。 。

N 2 of the number of X disks minus 1

Therefore: The number of moves: 2^n-1

Three Stack mechanism for calling methods: (Features: Advanced post-Exit)

Starting from the main thread to call the method (function) to keep the stack and the stack operation, the function call is the function of the stack, the end of the function is the process of the function out of the stack, so that the method calls to ensure the sequential flow, that is, when the function of multiple layers of nesting, from the outside to the inner layer of the function Finally, the function of the top of the stack executes at the end (the inner function executes the end first) out of the stack, and then the second-to-last function executes the end of the stack, and finally, the first stack of function calls is popped from the stack back to the main thread and ends.

Four Algorithm analysis (recursive algorithm):

When we use the computer to solve the problem of Hanoi Tower, an indispensable step is to analyze the algorithm of the whole realization. So far, the simplest algorithm to solve Hanoi tower problem is the same as the recursive return, as to what is recursive, recursive implementation of the mechanism is what, we say the simple point is that we are a method or function, but in their own this function has called itself the function of the statement, and this call how to call the end? , there must also be an end point, or, specifically, the function can return a certain value after the call to a certain time, and then the second to last can return a certain value, until the first call of this function can return a definite value.

The implementation of this algorithm can be easily divided into three steps:

(1) Move the n-1 plate from a to B;

(2) Move the nth plate from a to C;

(3) Move the n-1 plate from B to C;

From here, in addition to the above analysis of mathematical problem solving, we are not difficult to find that the number of steps moved must be odd steps:

(1) The middle step is to move the largest plate from a to c up;

(2) The middle step can be seen as a n-1 on a plate by the use of Auxiliary Tower (c tower) moved to B,

(3) The middle step can be regarded as a n-1 plate B on the use of Auxiliary Tower (a tower) moved to C;

Five, Java source code:

 Packagedemo;/*** Objective: To solve the problem of the Nottingham Tower * Dmego time: 2016-10-15*/ImportJava.util.Scanner; Public classTowersofhanoi {Static intm = 0;//mark Number of moves//functions to implement movement     Public Static voidMoveintDisksCharNCharM) {System.out.println ("++m" + "second move:" + "move" + disks+ "disc from" + N + "to" + "M); }    //recursive implementation of Hanoi functions     Public Static voidHanoiintNCharACharBCharC) {if(n = = 1)//when the disc is only one, just move it from tower A to Tower C.Towersofhanoi.move (1, A, C);//move the disc with the B number 1 from A to C        Else        {//otherwiseHanoi (N-1, A, C, B);//recursively, move the 1~n-1 disc on tower A to B and C as the auxiliary tower .Towersofhanoi.move (n, A, C);//move the N-numbered disc on tower A to C .Hanoi (N-1, B, A, C);//recursively, move the disc numbered 1~n-1 on Tower B to C and use a as an auxiliary tower .        }    }     Public Static voidMain (string[] args) {Scanner imput=NewScanner (system.in); CharA = ' a '; Charb = ' B '; Charc = ' C '; System.out.println ("******************************************************************************************"); System.out.println ("This is the Hanoi tower problem (put the small-to-large disc on tower A from tower A to the C tower through the B auxiliary Tower.")); System.out.println ("******************************************************************************************"); System.out.print ("Please enter the number of discs:"); intdisks =Imput.nextint ();        Towersofhanoi.hanoi (disks, A, B, C); System.out.println (">> moved" + M + "times to move the disc on A to C");    Imput.close (); }}

Six Diagram program Run Flow:

(1) function hanoi (int n,char A,char B,char C) is the function of a disk numbered N to move from A to C with B.

(2) function move (int n, char N, Char M) is the function of moving a 1-numbered disk from N to M

Seven Program run:

A graphical recursive algorithm for Hanoi

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.