Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
simple DFS solution in Speedy category for Open Labyrinth by knezi
def checkio(maze):
stack=[(10,10)]
maze[10][10]=2
while len(stack)!=0 and stack[-1] != (1,1): # from the end to the beginning
x,y=stack.pop()
add(x,y,1,0,maze,stack)
add(x,y,-1,0,maze,stack)
add(x,y,0,1,maze,stack)
add(x,y,0,-1,maze,stack)
x,y=(1,1)
path=""
while (x,y)!=(10,10):
direction,x,y=min([("N",x-1,y),("W",x,y-1),("S",x+1,y),("E",x,y+1)], key=lambda a: maze[a[1]][a[2]] if maze[a[1]][a[2]]>1 else 150)
maze[x][y]=150
path+=direction
return path
def add(x,y,a,b,maze,stack):
if maze[x+a][y+b]==0:
stack.append((x+a,y+b))
maze[x+a][y+b]=maze[x][y]+1
April 2, 2014
Comments: