Ideas are as follows:
To achieve the 3-order Hanoi solution, that is, when the initial state, a on the top from the bottom there are three plates, respectively, 1th Plate, 2nd plate and 3rd plate, of which 1th plate is the smallest, 3rd plate the largest;
Determine the number of remaining plates, if only one plate to exit the iteration, if there is more than one plate to continue the iteration.
The code is as follows:
Copy Code code 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 1th" + to);
else {//If there is more than one plate, continue the iteration
Movedish (Level-1, from, to, Inter);
System.out.println ("from" + from + "Move plate" + Level + "number" + to);
Movedish (Level-1, Inter, from, to);
}
}
public static void Main (string[] args) {
int ndisks = 3;//set Hanoi to 3-step
Movedish (ndisks, ' A ', ' B ', ' C '); implementation of mobile algorithm
}
}