博客
关于我
【BZOJ 1019】 1019: [SHOI2008]汉诺塔 (DP?)
阅读量:1026 次
发布时间:2023-04-17

本文共 2380 字,大约阅读时间需要 7 分钟。

为了解决这个问题,我们需要模拟汉诺塔的移动过程,根据给定的操作优先级,计算按照特定策略移动盘子所需的最小步骤数。我们将使用模拟器的方法来逐步模拟每一步操作,确保每次移动都符合规则。

方法思路

  • 初始化:将盘子从柱子A开始,逐层放置在A柱子上。
  • 优先级处理:按照给定的优先级顺序,依次检查每个操作是否可以执行。
  • 合法性检查:每次操作必须确保盘子的移动是合法的,盘子不能放在比它大的盘子上面,且不能移动上一次移动的盘子。
  • 更新状态:每次执行合法的操作后,更新各柱子的盘子堆、最上面盘子的位置以及记录上一次移动的盘子和柱子。
  • 循环直到完成:继续处理直到所有盘子都移动到目标柱子B或C。
  • 解决代码

    n = int(input())priority_str = input().split()A_pile = list(range(n, 0, -1))B_pile = []C_pile = []top = [n, 0, 0]pos = [0] * (n + 1)last_moved_disk = -1last_move_from = Nonelast_move_to = Nonecount = 0while True:    if top[0] == 0:        break    found = False    for op in priority_str:        src, dst = op[0], op[1]        # 检查源柱子是否有盘子        if src == 'A':            src_pile = A_pile        elif src == 'B':            src_pile = B_pile        else:            src_pile = C_pile        if not src_pile:            continue        x = src_pile[0]        # 检查是否是上一次移动的盘子        if last_moved_disk != -1 and x == last_moved_disk:            continue        # 检查目标柱子是否可以放下x        if dst == 'A':            dst_pile = A_pile        elif dst == 'B':            dst_pile = B_pile        else:            dst_pile = C_pile        if not dst_pile:            # 目标为空,可以放下x            pass        else:            y = dst_pile[0]            if y > x:                pass            else:                continue        # 执行该操作        # 移除x        if src == 'A':            A_pile.remove(x)        elif src == 'B':            B_pile.remove(x)        else:            C_pile.remove(x)        # 插入x到目标柱子        if dst == 'A':            A_pile.insert(0, x)        elif dst == 'B':            B_pile.insert(0, x)        else:            C_pile.insert(0, x)        # 更新top数组        if src == 'A':            top[0] = A_pile[0]        elif src == 'B':            top[1] = B_pile[0]        else:            top[2] = C_pile[0]        top[dst] = x        # 更新pos数组        if dst == 'B':            pos[x] = 1        elif dst == 'C':            pos[x] = 2        else:            pos[x] = 0        # 记录上一次移动的盘子和柱子        last_moved_disk = x        last_move_from = src        last_move_to = dst        count += 1        found = True        break    if not found:        continueprint(count)

    代码解释

  • 读取输入:读取盘子数量n和优先级字符串。
  • 初始化盘子堆:将盘子从下到上依次放在A柱子上。
  • 模拟器循环:直到A柱子为空为止,依次处理每个操作。
  • 优先级处理:从高优先级到低优先级检查每个操作是否可以执行。
  • 合法性检查:确保移动的盘子不是上一次移动的盘子,目标柱子可以放下盘子。
  • 更新状态:执行操作后,更新盘子堆、最上面盘子的位置和记录上一次移动的盘子。
  • 输出结果:当所有盘子都移动到目标柱子时,输出移动步骤数。
  • 转载地址:http://rmgfk.baihongyu.com/

    你可能感兴趣的文章
    No module named cv2
    查看>>
    No module named tensorboard.main在安装tensorboardX的时候遇到的问题
    查看>>
    No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
    查看>>
    No new migrations found. Your system is up-to-date.
    查看>>
    No qualifying bean of type XXX found for dependency XXX.
    查看>>
    No qualifying bean of type ‘com.netflix.discovery.AbstractDiscoveryClientOptionalArgs<?>‘ available
    查看>>
    No resource identifier found for attribute 'srcCompat' in package的解决办法
    查看>>
    no session found for current thread
    查看>>
    no such file or directory AndroidManifest.xml
    查看>>
    No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android
    查看>>
    NO.23 ZenTaoPHP目录结构
    查看>>
    no1
    查看>>
    NO32 网络层次及OSI7层模型--TCP三次握手四次断开--子网划分
    查看>>
    NOAA(美国海洋和大气管理局)气象数据获取与POI点数据获取
    查看>>
    NoClassDefFoundError: org/springframework/boot/context/properties/ConfigurationBeanFactoryMetadata
    查看>>
    node exporter完整版
    查看>>
    Node JS: < 一> 初识Node JS
    查看>>
    Node JS: < 二> Node JS例子解析
    查看>>
    Node Sass does not yet support your current environment: Windows 64-bit with Unsupported runtime(72)
    查看>>
    Node 裁切图片的方法
    查看>>