试图解决迷宫(波前算法)

时间:2013-12-16 08:44:45

标签: python algorithm wavefront

您好我正在尝试使用波形算法(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

1 个答案:

答案 0 :(得分:2)

x=0
y=0
while(x<len(floorMap[0])):
    while(y<len(floorMap)):
        ...
        y+=1
    x+=1

这里的问题是,一旦内部循环耗尽了y值,它将耗尽,直到外部循环完成,因为你没有为每个内循环重置y。 / p>

只需将y=0行移动到外循环中:

x=0
while(x<len(floorMap[0])):
    y=0
    while(y<len(floorMap)):
        ...
        y+=1
    x+=1