思路:

《算法很美》——汉诺塔游戏(递归)_算法很美

package test_1;

public class Test_05 {
//递归
public static void main(String[] args) {
hanoiTower(3, "A", "B", "C");
}

static void hanoiTower(int N, String from, String to, String help) {
if (N == 1) {
System.out.println(" move " + N + " from " + from + " to " + to);
return;
}
hanoiTower(N - 1, from, help, to);
System.out.println(" move " + N + " from " + from + " to " + to);
hanoiTower(N - 1, help, to, from);
}
}

运行结果:

《算法很美》——汉诺塔游戏(递归)_算法很美_02