河内塔 - 用 Python 解决中途算法

2024-01-03

河内塔有可能中途解决吗?我已经做了广泛的研究来寻找可以半途解决用户配置的代码,但我还没有找到。这是一项作业,我需要代码从用户停止解决的地方接管并继续为用户解决它,而不将谜题重置为一。

我知道有现成的递归算法,但这不是我正在寻找的。 我正在寻找可以从用户已经解决的地方接管,然后从那里继续解决的算法。 有任何想法吗?

到目前为止,我已经提出了一种算法,它将优化算法(通过递归完成)存储到一个数组中,然后检查用户的输入是否等于数组中找到的任何输入,然后从那里继续求解。然而问题就出在优化算法数组中找不到用户的配置。

以下是到目前为止我的代码(我已排除 stack.py 代码):

def solveHalfway(n, start, end, middle, count):
    gameInstance.stackA = [3,2]
    gameInstance.stackB = []
    gameInstance.stackC = [1]
    loopCounter = 0 # initialise loopCounter as 0
    moveCounter = 0 # initialise the move index the user is stuck at
    indicator = 0 # to indicate whether the user's config equals the solution's config
    while loopCounter < arrayOfStacks.size(): # while loopCounter size has not reached the end of arrayOfStacks
        if loopCounter != 0 and loopCounter % 3 == 0:  # if 3 stacks have been dequeued
            moveCounter += 1
            if gameInstance.getUserConfig() == tempStack.data:  #check whether user's config is equal to the solution's config
                indicator += 1
                print "User is stuck at move: ", moveCounter  #this will be the current move the user is at
                while arrayOfStacks.size() != 0: # while not the end of arrayOfStacks
                    correctMovesStack.push(arrayOfStacks.dequeue())  # add the moves to correctMovesStack
                    if correctMovesStack.size() == 3: # if 3 stacks have been dequeued
                        print "Step:", moveCounter , correctMovesStack.data # display the step number plus the correct move to take
                        moveCounter+=1 # increase move by 1
                        while correctMovesStack.size() != 0: # if correct moves stack isn't empty
                            correctMovesStack.pop() # empty the stack
                return
            else:
                while tempStack.size() != 0: # check if tempStack is empty
                    tempStack.pop()  # empty tempStack so that it can be used for the next loop
            tempStack.push(arrayOfStacks.dequeue()) #dequeue from arrayOfStacks for a total of 3 times and push it to tempStack
        else:
            tempStack.push(arrayOfStacks.dequeue()) #dequeue from arrayOfStacks for a total of 3 times and push it to tempStack
        loopCounter +=1 # increase loop counter by 1
    if indicator == 0:
        moveWith3Towers(noOfDisks, stackA, stackC, stackB, count)
    print indicator

要从任意位置求解河内塔,您可以使用类似于从标准起始位置开始工作的标准解决方案的递归过程。

它只是需要更通用一点。

编写一个递归过程moveDisks(最大大小,targetPeg)移动所有大小 maxSize到挂钩目标挂钩, 像这样:

  1. 找到最大的磁盘m这样m.size and m is not on 目标挂钩。如果没有这样的磁盘,则返回,因为所有大小maxSize已经在正确的地方了。

  2. Let 来源挂钩成为钉子m目前是,并且让otherPeg成为那个不是的钉子来源挂钩 or 目标挂钩.

  3. Call moveDisks(m.size-1, otherPeg)递归地将较小的磁盘移开。

  4. Move m from 来源挂钩 to 目标挂钩.

  5. Call moveDisks(m.size-1, targetPeg)递归地将较小的磁盘放在它们所属的位置。

在Python中,我会这样写。请注意,我对游戏状态使用了不同的表示形式,该表示形式更适合该算法,并且不允许任何非法位置:

#
# Solve Towers of Hanoi from arbitrary position
#
# diskPostions -- the current peg for each disk (0, 1, or 2) in decreasing
#                 order of size.  This will be modified
# largestToMove -- move this one and all smaller disks
# targetPeg -- target peg for disks to move
#
def moveDisks(diskPositions, largestToMove, targetPeg):
    for badDisk in range(largestToMove, len(diskPositions)):

        currentPeg = diskPositions[badDisk]         
        if currentPeg != targetPeg:
            #found the largest disk on the wrong peg

            #sum of the peg numbers is 3, so to find the other one...
            otherPeg = 3 - targetPeg - currentPeg

            #before we can move badDisk, we have get the smaller ones out of the way
            moveDisks(diskPositions, badDisk+1, otherPeg)

            print "Move ", badDisk, " from ", currentPeg, " to ", targetPeg
            diskPositions[badDisk]=targetPeg

            #now we can put the smaller ones in the right place
            moveDisks(diskPositions, badDisk+1, targetPeg)

            break;

Test:

> moveDisks([2,1,0,2], 0, 2)
Move  3  from  2  to  0
Move  1  from  1  to  2
Move  3  from  0  to  1
Move  2  from  0  to  2
Move  3  from  1  to  2
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

河内塔 - 用 Python 解决中途算法 的相关文章

随机推荐

  • 卡夫卡消费者寻求开始

    我没有使用分区来发布到 Kafka 主题 ProducerRecord 字符串主题 K键 V值 在消费者方面 我想从头开始 eekToBeginning 集合分区 是否可以在不使用分区的情况下寻求开始 Kafka 是否分配默认分区 http
  • 如何使 qtip 工具提示随光标移动

    我正在使用 js 库 qtip 工具提示 当我将鼠标悬停在表格中的悬停行上时 我想让 qtip 工具提示随光标移动 我知道如何让我自己的工具提示随光标移动 但我在使用 qtip 时遇到了困难 请解释您回答的代码 谢谢 My html tab
  • 类型转换为布尔值

    有人可以解释一下为什么会这样吗 var dump bool 1 2 returns bool true but var dump 1 2 returns bool false 当然第二次返回是正确的 但是为什么第一次 php 返回一个意外的
  • 黑客已将内容添加到我的 PHP 文件中 [已关闭]

    Closed 这个问题是无关 help closed questions 目前不接受答案 我的网站已被黑客拿下 浏览该网站会发现每个 PHP 文件的顶部都有大量附加内容 现在每个文件都以以下内容开头 GLOBAL wehaveitagain
  • 在 .js.erb 文件中使用 $(this) - Ruby on Rails AJAX

    我正在使用 Rails3 和 jQuery 并尝试执行简单的 ajax 调用 我有一个显示应用程序当前状态 在线 离线 的链接 单击后 它将更新状态 link to app status controller gt apps action
  • 如何在conda中管理两个pip版本?

    我正在 Windows 中使用 conda 我不小心安装了两个版本的 pip 使用python m pip install upgrade pip 现在当我跑步时conda list来自基础环境 While pip version给出点 1
  • 实体框架 - 使用 order by 和 group by 的 Linq 查询

    I have Measurement具有相关属性的对象CreationTime 日期时间 和Reference 字符串 和一些其他值 我想编写一个高效的 linq 查询DbContext that 分组我的Measurement给定的对象R
  • 在 eclipse 2.0 的 aws 工具包中承担/切换角色

    我正在使用适用于 eclipse 2 0 的 aws 工具包 使用选项 窗口 gt 首选项 gt aws 工具包 我已经配置了 IAM 登录用户 api 访问密钥 id 和秘密访问密钥 根据我们的 aws 配置 此 IAM 用户必须承担角色
  • 如何在 PHP 中查找图像是否存在或渲染正常?

    我遇到这种情况 我有一些图片 http www example com test1 jpg http www example com test2 jpg http www example com test3 jpg 其中一些可能是死链接 图
  • Spark:将 RDD 结果写入文件系统很慢

    我正在使用 Scala 开发 Spark 应用程序 我的应用程序仅包含一项需要改组的操作 即cogroup 它在合理的时间完美运行 我面临的问题是当我想将结果写回文件系统时 由于某种原因 它比运行实际程序花费的时间更长 起初 我尝试在不重新
  • 检查正在运行的程序中是否存在内存泄漏

    出于好奇 我有一个关于检查内存泄漏的问题 作为一个用过的人valgrind在过去的一两年里 我经常检查代码中的内存泄漏 我突然想到它只检测丢失 未释放的内存来世之后的程序 因此 鉴于此 我在想如果你有一个长期运行的程序malloc 是间歇性
  • C 中允许重复的 const 限定符,但 C++ 中不允许?

    示例代码片段 const const const int x 10 int main 在 C 中编译 但在 C 中不编译 为什么用C编译 我认为这在 C 中也会失败 没关系 C 标准的哪一部分禁止使用重复项constC 标准的哪一部分允许这
  • 如何对 fgets 使用 feof 和ferror(C 中的 minishell)[重复]

    这个问题在这里已经有答案了 我已经编写了这个 minishell 但我不确定我是否对错误进行了正确的控制 我知道 fgets 可以返回 feof 和ferror http www manpagez com man 3 fgets http
  • spring事务超时可配置

    我有一个具有固定超时的事务方法 有没有一种方法可以通过即配置来配置事务超时application yml Transactional propagation Propagation REQUIRED timeout TIMEOUT publ
  • 如何使用 Snowflake SQL 解析 ISO 8601 时间戳?

    我正在寻找一个允许我解析 ISO8601 时间戳的通用函数 我知道关于to timestamp tz https docs snowflake net manuals sql reference functions to timestamp
  • 将两个整数合并为一个并稍后解码

    使用 C 我需要将两个不同的 ID 组合成一个 16 位整数 然后我需要将这个 16 位整数解码为两个原始 ID 值 Example Store two integers into one unsigned short Identifier
  • 测试:如何测试视图包含所需的数据

    假设厨师可以制作食谱 副厨师可以创建必须经过主厨批准的食谱 您想要测试一下 当主厨查看她的主页时 她会看到她自己创建的食谱 您还想测试她是否看到有食谱等待她的批准 我可以想到两种方法来做到这一点 测试视图是否包含某些单词 例如 您的食谱 和
  • 当我使用与 SeismicXML 示例相同的 NSXMLParser 时出现内存泄漏问题

    我已经完成了与 SeismicXML 示例相同的 xml 解析 但现在它给了我内存泄漏问题 当我用仪器测试 SeismicXML 时 它也给出了相同的内存泄漏 在SeismicXML中 有EarthQuake示例 它包含来自xml解析的所有
  • String.find 始终返回 true (C++)

    我试图让布尔型found word 在找到单词 字符时返回 true 如果没有找到则返回 false 但无论我在文本中写什么 它总是返回 true 循环本身有效 已经尝试过了 包括 IOStream 和字符串 while timestoru
  • 河内塔 - 用 Python 解决中途算法

    河内塔有可能中途解决吗 我已经做了广泛的研究来寻找可以半途解决用户配置的代码 但我还没有找到 这是一项作业 我需要代码从用户停止解决的地方接管并继续为用户解决它 而不将谜题重置为一 我知道有现成的递归算法 但这不是我正在寻找的 我正在寻找可