In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces Java SE how to solve the Hanoi Tower problem, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.
1. Problem description
The Tower of Hanoi problem is a classic problem. The Tower of Hanoi (Hanoi Tower), also known as Hanoi Tower, originates from an ancient Indian legend.
When Brahma created the world, he made three diamond pillars, on which 64 gold discs were stacked from bottom to top.
Brahma ordered the Brahmin to rearrange the disc on another pillar in order of size from below.
It is also stipulated that the disk cannot be magnified on the small disc at any time, and only one disk can be moved between the three columns at a time. Ask how it should be done?
two。 Drawing analysis
The case of a disk: before moving
After moving
1 plate: a moves directly to C
The case of two discs: before moving
After moving
2 discs: a-> BA-> CB-> C.
The case of three discs: before moving
After moving
Three disks: a-> CA-> BC-> BA-> CB-> AB-> C-Amurc
3. Problem explanation
When there are three plates, you will find a problem. You must first move the top two plates to column B, then move the bottom plate to column C, and finally move the plate of column B to column C. The same is true for four plates, first move the top three plates to column B, move the bottom plate to column C, and finally move the plate of column B to column C. In this way, we have an idea that no matter how many plates there are, we should first move the n-1 plate to the B column, the bottom plate to the C column, and finally the B column plate to the C column.
Let's first take a look at the pattern:
1 plate: a-> C 1 time
2 plates: a-> BA-> CB-> C 3 times
3 plates: a-> CA-> BC-> BA-> CB-> BB-> AB-> C-A-C 7 times
So you can see that the number of moves is actually 2 ^ n-1 (n is the number of plates).
4. Code to achieve ublic class TestDemo {/ / first to write a function to simulate the mouse movement process, we have to print out the whole process of movement / / this move function is to move from 1 position to 2 position, it may be A-> BMagi A-> Cmai B. Various possible public static void move (char pos1,char pos2) {/ / so you only need to pass the corresponding location here to System.out.print (pos1+ "- >" + pos2+ ""). / / pos1 moves to pos2} / * * @ param n n represents the number of your plates * @ the location of the param pos1 plate * @ the transit position of the param pos2 plate * @ the end of the param pos3 plate * / public static void hanio (int ndjachar pos1,char pos2,char pos3) {if (n = = 1) {move (pos1,pos3) / / if there is only one plate, move from the A pillar to the C column} else {hanio (nMul 1 magistrate pos3 hanio); / / here a plate is moved from the A pillar to the B pillar move (pos1,pos3); / / the last plate left under the column is moved from the A pillar to the C pillar hanio. / / here is to move a plate from the B column to the C column}} public static void main (String [] args) {hanio (1 hanio); / / at the beginning of our Hanoi Tower to specify the position we passed to it for the first time, System.out.println (); hanio (2) System.out.println (); hanio (3); System.out.println ();
Print the results:
Thank you for reading this article carefully. I hope the article "how to solve the Hanoi Tower problem in Java SE" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.