Hanoi Tower Problem

Source: Internet
Author: User

The Hanoi tower problem [also known as Hanoi] is an ancient legend in India.

It is rumored that the epoch-making God Blama left three diamond rods in a temple, the first is covered with 64 round gold pieces, the largest one at the bottom, the other one smaller than a small, sequentially stacked up, the temple of the monks tirelessly to move them from this stick to another stick, the provisions can be used in the middle of a stick as a help, But only one can be moved at a time, and the big cannot be placed on the small. It is this seemingly simple problem, but it has troubled people for more than thousand years.

Later, the legend evolved into a Hanoi game, play as follows:

1. There are three Poles a,b,c. There are several plates on the A-pole.
2. Each time you move a plate, the small one can only be stacked on top of the big one.
3. Move all the plates from the A to the C-bar

Problem-solving thinking: Only three towers were given, and we used C tower to pile the discs in Tower B. First, the tower 1th is placed in the B tower, a tower 2nd disk in the C tower, and then placed in the B Tower of the 1th disk in C tower, the C tower has two discs as required from the bottom up from small to large arrangement. Next will be a tower of the 3rd disk in B Tower, the C Tower of the 1th disk in the B tower, the C-Tower 2nd disc in a tower, and then the B-tower 1th disk in a tower, when the C tower empty, 1th No. 2nd is ordered in a tower, b tower only 3rd disc. At this time, the B tower 3rd disk in C Tower, put a tower 1th in Tower B, a tower 2nd room in C Tower, and then the B tower 1th in C Tower, at this time b tower empty, C tower according to the requirements of the 123th disk. This time, the tower 4th is placed in Tower B, this time it will be more troublesome to put the C tower number 1th in Tower A. Tower 2nd in Tower B, and then put a tower 1th in Tower B, C Tower 3rd in a tower, and then put the B tower 1th in Tower C, the B tower 2nd in a tower, and then C-Tower 1th in a tower, when C tower is empty, B Tower only 4th disc, a tower according to the requirements of the room has 123 to n disc, missing 4th disc. Now put the B tower 4th disc room in Tower C, now push back, put a tower 1th room in C Tower, Tower 2nd in Tower B, and then put the C tower 1th in Tower B, a tower 3rd room C Tower, this time is just 3rd pressure 4th in C Tower, and then, b Tower 1th in a tower, the C Tower of 2nd placed in Tower C, Put the tower 1th in Tower C, this is just pushed back, at this time b tower empty, a tower is the top of the 5th disc, C tower according to the requirements of the 1234th disk.

According to this recursive method, the n-1 disk is placed on the C tower, the nth disc is placed in Tower B, and a tower is now empty. The n disk is the largest disc, according to the question we finally put the largest disk in the B tower, the use of an empty a tower combined with the BC Tower pushed back, you can put n disks as required in the B tower.

1 Import Java.io.BufferedReader; 2 Import Java.io.InputStreamReader; 3  4 public class Hanoi {5 public     static void Main (String args[]) throws Exception {6         int n; 7         Bufferedre Ader buf =  8                 new BufferedReader (New InputStreamReader (system.in)); 9         System.out.print ("Please enter the number of disks:");         n = integer.parseint (Buf.readline ());         Hanoi Hanoi = new Hanoi ();         hanoi.move (n, ' A ', ' B ', ' C ');     }14 15 Public     void Move (int n, char A, char B, char c) {         if (n = = 1)             System.out.println ("disk" + N + "by" + A + "Move to" + C), or         else {move             (n-1, A, C, b),             System.out.println ("disk" + N + "from" + A + "to" + C); 21< C17/>move (n-1, B, A, c);         }23     }24}

Hanoi Tower Problem

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.