It is said that there is a temple on the mountain , There is a little monk and an old monk in the temple . One day, the little apprentice had nothing to do , Go to the old monk , Ask if there are any games that can exercise intelligence , The old monk prepared three pillars for the young monk , say : First of all, there is only one bowl on the first pillar , You move the bowl to the third pillar , The little monk finished it very easily ; Then the old monk put three bowls on the first pillar , And the three bowls are put in order from small to large , This time he asked the young monk to move the three bowls from the first pillar to the third pillar , But in the process of moving, make sure that the small bowl is above the big one , And only one bowl can be moved at a time , The little monk thought for a moment , Achieved the goal ; But in the end, the old monk said if I gave it to you 64 How about a bowl ?
The little monk fell into thinking . Smart you know how to do it ?
Hanoi (Hanoi
Tower), Also known as Hanoi tower , Originated from an ancient Indian legend . Brahma made three diamond pillars when he created the world , They are stacked on a column from bottom to top in order of size 64 A golden disc . Brahman ordered Brahman to rearrange the disk on another pillar in order of size from below . And the rules , anytime , You can't enlarge the disk on a small disk , And only one disc can be moved between the three pillars at a time . How to operate ?
1, Move a plate : (1 step )
Kids know that , Of course, just put it there A - - > C
2, Move two plates :(3 step )
A1- - > B , A2 - - > C :
A1 - - > C :
3, Move three plates : (7 step )
A1 - - > C , A2 - - > B :
A1 - - > B , A3 - - > C :
A1 - - > A , A2 - - > C , A1 - - > C
We summarize the above rules :
Move a plate : 1 step = 2^1 - 1
Move two plates : 3 step = 2^2 - 1
Move three plates : 7 step = 2^3 - 1
…
Move 64 A plate : 2^64 - 1
Let's make a simple calculation , Computer operation 64 A plate has to run for nearly a year 300 Year or so
So let's simply output it 1,2,3 The result of a plate
package test24; public class testDemo { public static void move (char pos1,char
pos2) { System.out.print(pos1+" --> "+pos2+" ");// Imitate the movement of the mouse } /** * * @param n
Represents the number of plates * @param pos1 Represents the first pillar A * @param pos2 Represents the second pillar B * @param pos3 Represents the third pillar C
*/ public static void HanoiTower (int n,char pos1,char pos2,char pos3) {
//n Represents the number of plates if(n == 1) { move(pos1,pos3); } else { HanoiTower(n-1,pos1,pos3,pos2)
; // pos1 Things on the wall pass through pos3 Spread to pos2 upper move(pos1,pos3); HanoiTower(n-1,pos2,pos1,pos3);
} } public static void main(String[] args) { HanoiTower(1,'A','B','C'); System.
out.println(); HanoiTower(2,'A','B','C'); System.out.println(); HanoiTower(3,'A'
,'B','C'); System.out.println(); } }
Running results :
I want to try 64 It's a plate , You can try it yourself !!!
Technology