Java recursive implementation of the question of the Han-nuo tower

Source: Internet
Author: User

Hanoi Tower problem is: There are ABC three pillars, a pillar from top to bottom stacked a lot of volume in turn, if the disk from a to C pillars, and still keep from top to bottom in order to increment.

Class hanio{public void Moveone (int n, string init, String desti) {    //The case of only one plate System.out.println ("Move:" +n+ "from" + init+ "to" +desti);} public void Move (int n, string init, String temp, string desti) {if (n <= 0) {System.out.println ("number Error"), return; else if (n = = 1) {moveone (n,init,desti);} Else{move (n-1, init, Desti, temp);//First move the upper (n-1) tray from the init lever to the temp bar  desti (n, Init, moveone) with the desti lever;     Then move the plate numbered n from the init lever to the desti rod  Move (n-1, temp, init, desti), and/or finally move the upper (N-1) plate from the temp lever to the desti lever with the init lever   }}}class Hanioapp{public static void Main (String args[]) {Hanio Hanio = new Hanio (); Hanio.move (3, "A", "B", "C");}}
Operation Result:

Move:1 from A to C move:2 from a to B move:1 from C to B move:3 from A to C move:1 from B to A Move:2 from B to C Move:1 F Rom A to C



Java recursive implementation of the question 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.