【无标题】

from collections import deque

def min_minutes(N, K):
    if N >= K:
        return N - K
    MAX = 200000  # 安全上界
    dist = [-1] * (MAX+1)
    q = deque()
    dist[N] = 0
    q.append(N)
    while q:
        x = q.popleft()
        if x == K:
            return dist[x]
        # 三种移动
        for nx in (x-1, x+1, x*2):
            if 0 <= nx <= MAX and dist[nx] == -1:
                dist[nx] = dist[x] + 1
                q.append(nx)

# 交互示例:从标准输入读入两个整数 N K
if __name__ == "__main__":
    N, K = map(int, input().split())
    print(min_minutes(N, K))

def hanoi(n, src, aux, dst, moves):
    """
    将 n 个盘子从 src 移到 dst,借助 aux。
    盘子编号按 1..n(1 最小)。
    moves 为列表,记录每一步的字符串。
    """
    if n == 0:
        return
    # 先把上面 n-1 个盘子从 src 移到 aux
    hanoi(n-1, src, dst, aux, moves)
    # 再把第 n 个(即当前最大的)从 src 移到 dst
    moves.append(f"移动圆盘 {n} 从柱子 {src} 到柱子 {dst}")
    # 最后把 n-1 个从 aux 移到 dst
    hanoi(n-1, aux, src, dst, moves)

def print_hanoi_moves(n):
    moves = []
    hanoi(n, 'A', 'B', 'C', moves)
    for line in moves:
        print(line)

if __name__ == "__main__":
    # 测试:修改 n 值即可
    n = int(input().strip())
    print_hanoi_moves(n)

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值