Data structure (Java language description) recursive implementation--Hanoi tower problem

Source: Internet
Author: User

1. Hanoi Tower Problem Description

N-Order Hanoi: Suppose there are 3 three tower blocks named X, Y, z, with n plates on X., different diameters, small to large, ... n arrangement, to use Y to transfer n plates to Z, during which the small plates cannot be pressed on a large plate. Rules:

    • To move one plate at a time;
    • The plate can be inserted in any one of the x, Y, z Towers;
    • The market cannot be pressed on a small plate at any time.
2. Thinking of solving problems

When N=1, direct the plate from the x-->z;

When n>1, you need to use Y, First will (N-1) a plate by x-->y, the nth implementation x-->z, and then the problem to achieve (n-1) a plate from y-->z, with the help of X;

To implement n plates from X to Z, first move n-1 to Y, move Nth to Z, and then use Z to move the n-1 plate back to X.

The loop implements (N-1) a plate from X to Z.

Note: The initial recursion involves moving the plates with move () to achieve the largest one on the plate, where you can sit and mark.

3. Code

Package Hanoi;
public class Hanoi {
    private int c=0;
    public void Hanoi (int n,char X,char Y,char z) {
        if (n==1)
        {
            move (x,1,z);
        }else
        {
             hanoi (n-1,x,z,y);
            move (x,n,z);
            hanoi (n-1,y,x,z);
        }
    }    
public void Move (char x,int N,char z) {
     System.out.println ("First" + ++c + "second move:" +n+ "Plate," +x+ "--" +z ");
}
public static void Main (string[] args) {
    hanoi e=new Hanoi ();
    e.hanoi (5, ' x ', ' y ', ' z ');
}
}

When n=4, the result of the operation:

1th MOVE: Plate No. 1th, X-->y
2nd MOVE: Plate No. 2nd, X-->z
3rd MOVE: Plate No. 1th, Y-->z
4th MOVE: Plate No. 3rd, X-->y
5th move: Plate No. 1th, z-->x
6th MOVE: Plate No. 2nd, Z-->y
7th MOVE: Plate No. 1th, X-->y
8th MOVE: Plate No. 4th, X-->z
9th move: Plate No. 1th, Y-->z
10th MOVE: Plate No. 2nd, y-->x
11th MOVE: Plate No. 1th, z-->x
12th MOVE: Plate No. 3rd, Y-->z
13th MOVE: Plate No. 1th, X-->y
14th MOVE: Plate No. 2nd, X-->z
15th MOVE: Plate No. 1th, Y-->z

Data structure (Java language description) recursive implementation--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.