我正在處理一個問題,處理河內問題的塔上的變化,其中一個只能移動到相鄰的樁,我們只限於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)