The idea is as follows:
To realize the solution steps of the third-order Hannover Tower, that is, in the initial state, there are three plates from top to bottom on A, namely, disk 1, disk 2 and disk 3, of which disk 1 is the smallest and disk 3 is the smallest The largest plate;
Determine the number of remaining plates. If there is only one plate, exit the iteration. If there is more than one plate, continue iteration.
The code is as follows:
The code copy is as follows:
public class HanoiTower {
public static void moveDish(int level, char from, char inter, char to) {
if (level == 1) {// If there is only one plate, exit the iteration
System.out.println("from" + from + "Move plate number 1 to" + to);
} else {// If there is more than one plate, continue iterating
moveDish(level - 1, from, to, inter);
System.out.println("from" + from + "Move the plate" + level + "sign to" + to);
moveDish(level - 1, inter, from, to);
}
}
public static void main(String[] args) {
int nDisks = 3;// Set Hannover Tower to 3rd order
moveDish(nDisks, 'A', 'B', 'C');// Implement the moving algorithm
}
}