2
你好,我正在嘗試使用波形算法(explained here)在python中編寫迷宮求解程序。我正在使用嵌套循環來遍歷矩陣並更改數字,如in this animated gif.試圖解決迷宮問題(波前算法)
但是,它似乎只是循環遍歷矩陣的第一行,而不是繼續前進到其餘。我一直在盯着這件事很長一段時間,我希望能對它有所幫助。
感謝,洛根
floorMap = [[000,000,000,000,000,999,999,999,999,999],
[000,000,999,000,999,000,000,000,999,999],
[000,000,999,000,999,000,000,000,999,999],
[000,000,000,000,999,000,000,000,999,999],
[999,000,000,000,999,000,999,999,999,999],
[999,000,000,000,000,000,999,000,000,999],
[999,000,000,000,999,999,999,000,000,999],
[999,000,999,000,000,000,999,000,000,999],
[999,000,999,999,999,000,000,000,000,999],
[999,999,999,999,999,999,999,999,000,000]]
robotX=0
robotY=0
goalX=9
goalY=9
currentNum=0
wall=999
uncalculated=000
floorMap[robotX][robotY]=1
def changeSurroundings(X, Y):
#left
if(floorMap[X-1][Y]==000):
floorMap[X-1][Y]=currentNum
#right
if(floorMap[X+1][Y]==000):
floorMap[X+1][Y]=currentNum
#up
if(floorMap[X][Y-1]==000):
floorMap[X][Y-1]=currentNum
#down
if(floorMap[X][Y+1]==000):
floorMap[X][Y+1]=currentNum
def printMap():
i=0
while(i<len(floorMap)):
print floorMap[i]
print ""
i+=1
print ""
print ""
#------------------THIS IS WHERE THE PROBLEM IS--------------
while(floorMap[goalX][goalY]==0):
x=0
y=0
while(x<len(floorMap[0])):
while(y<len(floorMap)):
if(floorMap[x][y] > 000 and floorMap[x][y] < 999):
currentNum=floorMap[x][y]+1
changeSurroundings(x,y)
printMap()
y+=1
x+=1