The recursive algorithm of the Han Nuo tower

Source: Internet
Author: User

Recursive definition:

Recursive algorithm is a sub-problem that transforms a problem into a similar problem with reduced scale. The function (or procedure) is then called recursively to represent the solution to the problem.

A procedure (or function) calls itself directly or indirectly, a process (or function) called a recursive procedure (or function).

The recursive algorithm solves the problem characteristic:

(1) Recursion is the invocation of itself in a procedure or function.

(2) When using a recursive strategy, there must be a definite recursive end condition called a recursive exit.

(3) Recursive algorithm is usually very concise, but the recursive algorithm is less efficient in solving problems. Therefore, the recursive algorithm is generally not advocated for the design of the program.

(4) in the process of recursive invocation, the system opens up a stack for each layer's return point, local quantity and so on to store. Too many recursion times can cause stack overflow and so on. Therefore, the recursive algorithm is generally not advocated for the design of the program.

Example: Hanoi

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

Code implementation:

//TOPN Plate number,from--> initial tower seat inter--> Auxiliary tower seat to--> final tower seat     Public Static voidDotower (intTopN,CharFromCharInterCharTo ) {        if(TopN = = 1) {System.out.println ("Plate 1, from the" + from + "tower to the" + to + "tower seat"); } Else{dotower (TopN-1, from, to, Inter); System.out.println ("Plate" + TopN + ", from" + from + "tower to" + to + "tower seat"); Dotower (TopN-1, Inter, from, to); }    }

The recursive algorithm of the Han Nuo tower

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.