Hanoi Tower Problem: There was a Buddhist tower in ancient times, with three A, B, and C in the tower. There were n plates on the A. The plates vary in size, the larger ones are at the bottom and the smaller ones are at the top (as shown in the picture).
There was a monk who wanted to move these n plates from seat A to seat B, but only one plate was allowed to move at a time. During the movement, the plates on the three seats always kept the large plates under and the small plates on the top. During the movement process, you can use the B seat to require printing the movement steps. If there is only one plate, you do not need to use the B seat and move the plate directly from A to C.
The Java code is as follows:
public class Hanoi { public static void main(String[] args) { int disk = 3; //Dish move(disk, 'A', 'B', 'C'); } /* * According to the question, number from top to bottom => 1 ~ n */ /** * * @param topN The plate number at the top of the source tower* @param from which tower is moved from * @param inter agent, 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 " + 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 Disk 2 from B to C Disk 1 from A to C