JAVA Implementation of Jakarta)

Source: Internet
Author: User

 

 

Improved the recursion part of the blog code I posted in 6.2.

The printing method is called in the movethetopone () method. Before calling this method, print the direction of movement-from Tower X to Tower y

The tower requires that all the plates on the first tower be carried to the third tower with the help of the second tower.

Rule: Only one plate can be moved at a time.

 

 

// Linglong Tower
Public ClassNewhanoi {
   Public Static IntTiers = 4;// Tiers Layers
    Private StaticList <string> pagoda1 =NewArraylist <string> (); // static pointer
    Private StaticList <string> pagoda2 =NewArraylist <string> ();

    Private StaticList <string> pagoda3 =NewArraylist <string> ();

   // Ing, used to determine and print the sequence number of the tower (using the badge), you can also use Map

   Private Static list [] mapping = {pagoda1, pagoda2, pagoda3 };


    Public Static VoidMain (string [] ARGs ){
       Preparepagda (pagoda1, tiers);
       System.Out. println ("initial state :");
       Printpagodas ();
        Hanoi (tiers, pagoda1, pagoda2, pagoda3 );
       System.Out. println ("Final Result :");
       Printpagodas ();
   }
   // -- Prepare the plate (add-string) (source Tower)
   Private Static VoidPreparepagda (list <string> srcpagda,IntTiers ){

       // Container used to assemble the tower Layer
       StringbuilderBuilder =NewStringbuilder ();

       //Add plates to each layer of the source tower. Starting from the bottom layer, ''indicates the diameter of the 'plate, equal"^" Count
       For(IntI = tiers; I> 0; I --){           

           //Each layer consists of 2 * Tiers-1 grids, which represent the size of the plate. "^" grids are separated by spaces.
           For(IntK = 0; k <Tiers-I; k ++) builder. append (""); // the space on the left of the plate, quantity: [2 * Tiers-1-(2 * i-1)]/2 = Tiers-I, same on the right
           For(IntJ = 1; j <= 2 * I-1; j ++ ){      //Number of Plates
               If(J % 2 = 1) builder. append ("^ ");// Place at intervals

               ElseBuilder. append ("");
           }
           For(IntK = 0; k <Tiers-I; k ++) builder. append ("");// Space on the right of the plate

           Srcpagda. Add (builder. tostring ());  //Add to Tower
           Builder. Delete (0,Builder. Length ()); //Clear the container before the next cycle
       }
   }
   // -- Print the current situation of the tower

   Private Static VoidPrintpagodas (){

        // The print layers are three towers-the maximum height of the current situation
       IntLen = math. Max (pagoda1.size (), math. Max (pagoda2.size (), pagoda3.size ()));
       // Used to display the blank layer of the-tower

       StringbuilderSpaces =NewStringbuilder ();
       Spaces. append ("-"); // -- Add the left outer frame of the tower
       For(IntI = 0; I <2 * Tiers-1; I ++) spaces. append (""); // blank layer is displayed with spaces
       Spaces. append ("-\ t"); // -- add the right outer frame of the tower and the interval between the towers       

       

       For(IntI = len-1; I> = 0; I --){//Starting from the top layer            

         // The layer of the three towers in the same horizontal plane is displayed in the same row.
           //If a tower does not have this layer, list. Get (INDEX) throws an out-Of-The-boundary exception and uses try-catch to handle it. A space is displayed on this layer. 
           Try{System. Out. Print ("-" + pagoda1.get (I) + "-\ t ");
           }Catch(ExceptionE1) {system. Out. Print (spaces );
           }
           Try{System. Out. Print ("-" + pagoda2.get (I) + "-\ t ");
           }Catch(ExceptionE) {system. Out. Print (spaces );
           }
           Try{System. Out. Print ("-" + pagoda3.get (I) + "-\ t ");
           }Catch(ExceptionE) {system. Out. Print (spaces );
           }
           System. Out. Print ("\ r \ n ");
       }
   }
   // This method (recursive core method) moves from the specified source tower to the specified target tower.

   Public Static VoidHanoi (IntMovenum, list <string> from, list <string> middle, list <string> ){
       If(Movenum = 1 ){//Recursively move a plate, use the move Method 
           Movethetopone (from,);
           Return;
       }

        //The implementation is divided into three steps: 1. Move all the plates above the source tower chassis to the middle tower (recursion); 2. Move the chassis to the target tower; 3, move all the plates on the middle tower to the target tower (recursion ).
       Hanoi (movenum-1, from, to, middle);
       Movethetopone (from, );
       Hanoi (movenum-1, middle, from, );
   }
   // The method name is his function
   Private Static VoidMovethetopone (list <string> from, list <string>To ){
       String thetopone = from. Remove (from. Size ()-1 );
       To. Add (thetopone );

      // Print the image. Each time you move it, the image is displayed.
        System. Out. println ("*********** print *********** \ r \ n ");
        // Determine the sequence number of the tower
        Int fromnum = 0, tonum = 0;
        For (INT I = 0; I <mapping. length; I ++ ){// Traverse the array of the tower
   
           If (mapping [I] = from ){
                Fromnum = I + 1;
           }
           If (mapping [I] = ){
                Tonum = I + 1;
           }
       }
        System. Out. println ("from" + fromnum + "Tower to" + tonum + "Tower \ r \ n ");

       Printpagodas (); // print the image
   }
}

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.