python汉诺塔算法_基于Python的汉诺塔求解中途算法
能不能把河內(nèi)塔半路解決?我做了大量的研究來尋找能夠解決用戶配置問題的代碼,但是我還沒有找到一個(gè)。這是一個(gè)任務(wù),我需要代碼接管從用戶已經(jīng)停止求解的地方,并繼續(xù)為用戶解決它,而不重置為方塊一。在
我知道有遞歸算法在那里隨時(shí)可用,但這不是我要尋找的。
我正在尋找算法,它可以接管用戶解決問題的地方,然后繼續(xù)從那里解決問題。
有什么想法嗎?在
到目前為止,我已經(jīng)提出了一種算法,它將優(yōu)化的算法(通過遞歸實(shí)現(xiàn))存儲(chǔ)到一個(gè)數(shù)組中,然后檢查用戶的輸入是否等于數(shù)組中找到的任何一個(gè),然后從那里繼續(xù)求解。但是,問題在于優(yōu)化算法數(shù)組中找不到用戶的配置。在
以下是我目前為止的代碼(我排除了堆棧.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
總結(jié)
以上是生活随笔為你收集整理的python汉诺塔算法_基于Python的汉诺塔求解中途算法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 荒野大镖客2剧情和1有关系吗(网易《荒野
- 下一篇: 啥也不干47W!耗时5个月 Intel显