你能解释一下使用堆栈追踪河内塔问题中的递归步骤的过程吗?你可以在这个网站上找到java代码:
http://www.javawithus.com/programs/towers-of-hanoi
Java拼图的递归程序如下:
public class TowersOfHanoi {
public void solve(int n, String start, String auxiliary, String end) {
if (n == 1) {
System.out.println(start + " -> " + end);
} else {
solve(n - 1, start, end, auxiliary);
System.out.println(start + " -> " + end);
solve(n - 1, auxiliary, start, end);
}
}
public static void main(String[] args) {
TowersOfHanoi towersOfHanoi = new TowersOfHanoi();
System.out.print("Enter number of discs: ");
Scanner scanner = new Scanner(System.in);
int discs = scanner.nextInt();
towersOfHanoi.solve(discs, "A", "B", "C");
}
}
非常感谢任何其他了解递归的补充材料。