汉诺塔
推导:
n代表盘子个数 ,source代表源柱子,dest代表目标柱子
if(n=1) {
move 盘子 from source to dest;
}else {
move n-1 盘子 from source to other;
move 最后一个盘子 from source to dest;
move n-1 盘子 from other to dest;
}
public class Hannoi {
// 0, 1, 2
public static void move(int n, int source, int dest) {
if (n == 1) {
System.out.printf("从 %d 移到 %d%n", source, dest);
return;
}
int other = 3 - source - dest;
move(n - 1, source, other);
System.out.printf("从 %d 移到 %d%n", source, dest);
move(n - 1, other, dest);
}
public static void main(String[] args) {
// String "11" -> int 11
move(Integer.parseInt(args[0]), 0, 2);
}
}