Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Simple BFS solution in Clear category for Open Labyrinth by ale1ster
from heapq import heappush as hpush, heappop as hpop
def checkio(maze_map):
move = lambda cur,d: (cur[0] + d[0], cur[1] + d[1])
(depth, state) = (0, ((1, 1), ''))
(hq, visited) = ([], set())
hpush(hq, (depth, state))
diffs = {(1, 0): 'S', (-1, 0): 'N', (0, -1): 'W', (0, 1): 'E'}
while state[0] != (10, 10):
depth += 1
(p, state) = hpop(hq)
if state[0] in visited:
continue
else:
visited.add(state[0])
for diff in diffs:
new = move(state[0], diff)
if (new not in visited) and (maze_map[new[0]][new[1]] != 1):
hpush(hq, (depth, (new, state[1] + diffs[diff])))
return state[1]
May 25, 2014