#54325: 解題報告 Python


fengme0110@gmail.com (鼠標)


def hanoi(n, source, target, auxiliary):
    """
    n: 盤子數量
    source: 起始柱子 (A)
    target: 目標柱子 (C)
    auxiliary: 輔助柱子 (B)
    """
    if n == 1:
        # 基礎情況:只有一個盤子時,直接移動
        print(f"Move disc 1 from {source} to {target}")
        return

    # 步驟 1: 將 n-1 個盤子從 A 移到 B (利用 C 當輔助)
    hanoi(n - 1, source, auxiliary, target)

    # 步驟 2: 將第 n 個盤子從 A 移到 C
    print(f"Move disc {n} from {source} to {target}")

    # 步驟 3: 將 n-1 個盤子從 B 移到 C (利用 A 當輔助)
    hanoi(n - 1, auxiliary, target, source)

try:
    n = int(input())
    # 呼叫函式:從 A 移到 C,中間利用 B
    hanoi(n, 'A', 'C', 'B')
except EOFError:
    pass