我正在研究一个处理河内塔问题的变体的问题,您只能移动到相邻的钉子,而我们仅限于 3 个钉子问题。我已经获得了打印出光盘数量所需移动的代码,但我无法弄清楚如何打印递归调用的数量。
def adjacent_hanoi(num_discs, start_peg, end_peg):
"""
Given the number of discs in Adjacent-Peg Tower of Hanoi:
1. Prints each move necessary to solve the puzzle (minimum number of moves)
2. Returns the total number of moves required
For this problem, discs should always start on the first peg and
end on the last peg.
num_discs: an integer number of discs
start_peg: starting peg
end_peg: ending peg
returns: an integer number of moves
"""
if num_discs > 0:
adjacent_hanoi(num_discs-1, start_peg, end_peg)
print "Move disc", num_discs, "from peg", start_peg, "to peg", 2
adjacent_hanoi(num_discs-1, end_peg, start_peg)
print "Move disc", num_discs, "from peg", 2 , "to peg", end_peg
adjacent_hanoi(num_discs-1, start_peg, end_peg)