Java code example for using recursion to solve the problem of _java

Source: Internet
Author: User

Hanoi (Hanoi) Tower problem: There is a Vatican tower in ancient times, the tower has three seats a, B, C,a seat on the N-plate, the size of the plate, large in the lower, small on the top (as pictured).

One monk wanted to move the N plate from block A to block B, but only allowed to move one plate at a time, and in the course of the move, the plates on the 3 seats kept the plate down and the small plate on. Block B can be used during the move, requiring the printing of moving steps. If you have only one plate, you do not need to use block B to move the plate directly from a to C.

    • If you have 2 plates, move the plate 2 on plate 1 to B, move the plate 1 to C, and move the plate 2 to C. This explains: You can use B to move 2 plates from A to C, and of course, you can also use C to move 2 plates from A to B.
    • If there are 3 plates, then according to the 2-plate conclusion, you can use C to move two plates from a plate 1 to B, to move the plate 1 from a to c,a into an empty seat, and a block to move the two plates on B to C. This means that you can move 3 plates from one seat to another with the help of an empty seat.
    • If there are 4 plates, the first one is to move the three plates from a to B from a plate 1, to move the plate 1 to the c,a into the empty seat, and to move the three plates on block B to C with the help of empty seat C.

The Java code is as follows:

public class Hanoi {public 
  
 static void Main (string[] args) { 
  int disk = 3;//Plate move 
  (disk, ' A ', ' B ', ' C '); 
   } 
  
 * 
  * According to the terms, from the top down number => 1 ~ n/ 
 /** 
  * 
  * * @param TOPN SOURCE Tower plate number 
  * @param from which tower move 
  * @param inter intermediary, Transition Tower 
  * @param to Destination tower * 
  * 
 private static void Move (int topn, char from, Char Inter, char to { 
  if (TOPN = 1) { 
   System.out.println (' Disk 1 from ' + from + ' to ' + to); 
  } else {move 
   (topN-1, from, to, inter); 
   System.out.println ("Disk" + topn + ' from ' + from + ' to ' + to); 
   Move (topN-1, Inter, from, to);}} 
   
 

Out

Disk 1 from A to C 
disk 2 from A to B disk 
1 from C to B disk 3 from A to 
C 
Disk 1 from B to a 
Di SK 2 from B to C 
Disk 1 from A to C 

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.