日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python汉诺塔算法_基于Python的汉诺塔求解中途算法

發布時間:2023/12/15 python 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python汉诺塔算法_基于Python的汉诺塔求解中途算法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

能不能把河內塔半路解決?我做了大量的研究來尋找能夠解決用戶配置問題的代碼,但是我還沒有找到一個。這是一個任務,我需要代碼接管從用戶已經停止求解的地方,并繼續為用戶解決它,而不重置為方塊一。在

我知道有遞歸算法在那里隨時可用,但這不是我要尋找的。

我正在尋找算法,它可以接管用戶解決問題的地方,然后繼續從那里解決問題。

有什么想法嗎?在

到目前為止,我已經提出了一種算法,它將優化的算法(通過遞歸實現)存儲到一個數組中,然后檢查用戶的輸入是否等于數組中找到的任何一個,然后從那里繼續求解。但是,問題在于優化算法數組中找不到用戶的配置。在

以下是我目前為止的代碼(我排除了堆棧.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

總結

以上是生活随笔為你收集整理的python汉诺塔算法_基于Python的汉诺塔求解中途算法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。