Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Dijkstra's Forever ! solution in Uncategorized category for Open Labyrinth by Miaou
def checkio(labyrinth):
# I'm definetely a fan of Dijkstra's (!!!)
# Object oriented Dijkstra: each cell contains 3 fields
# (and I will not use a class for that, just a tuple)
# which are: the distance from the beginning (cost),
# whether the cell is "explored" (path to this cell found),
# the cell before in the path from the begining.
assert len(labyrinth) == 12 # ;)
assert labyrinth[1][1] == 0 # ;)
assert labyrinth[10][10] == 0 # ^^
# 1) Init grid of cells.
grid = [[(None,False,None)]*12 for i in range(12)]
# 2) I will use a set to store the index of cell I touched but not
# explored yet. These are candidates to pursue my exploration.
# Dijkstra said : continue with the candidate which has the lowest cost.
sCandidates = { (1,1) } # Exploration starts from there.
grid[1][1] = (0,False,None) # Initial cell has cost 0 (beggining)
# 3) Main loop until destination is reached !
# (and while there are candidates. If no more candidates:
# bug because destination is not reachable)
while sCandidates and not grid[10][10][1]:
# 3.1) Pick best candidate (lowest cost)
i,j = min(sCandidates, key=lambda cand:grid[cand[0]][cand[1]][0])
sCandidates.remove((i,j))
# 3.2) Discover neighbors
for di,dj in [(-1,0),(1,0),(0,-1),(0,1)]:
# If neighbor is a hole, see next neigh
if labyrinth[i+di][j+dj] == 1: continue
# ... and if neigh is already explored too
if grid[i+di][j+dj][1]: continue
# If neighbor has never been seen before, then we record the path,
# and add it to the candidates
if grid[i+di][j+dj][0] == None: # == None is != 0 ;)
grid[i+di][j+dj] = (grid[i][j][0]+1,False,(i,j))
sCandidates.add((i+di,j+dj))
# but if neighbor is not already explored and the current path
# is shorter than the recorded path, then replace it
#if grid[i][j][1] + 1 < grid[i+di][j+dj][0]:
# (this case will never happen, due to the constant distance 1
# between the cells)
grid[i][j] = (grid[i][j][0],True,grid[i][j][2]) # (i,j) is explored
assert grid[10][10][1], "No Way Out found !!!"
# 4) Rollback path: each cell contains its precedence on the path from
# initial cell to this cell.
# We only have to see which cell is before destination and then before,
# and so on until we reach start.
i,j = 10,10
path = [] # Stores deltas
while (i,j) != (1,1):
pi,pj = grid[i][j][2]
path.insert(0,(i-pi,j-pj))
i,j = pi,pj
# 5) Translation of the path into a string
# (ok, here I chose the oneline solution...)
return ''.join( (di==-1 and 'N') or
(di==1 and 'S') or
(dj==-1 and 'W') or
(dj==1 and'E') for di,dj in path )
if __name__ == '__main__':
#Return any route to exit
print(checkio([
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1],
[1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1],
[1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1],
[1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 1, 1],
[1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1],
[1, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1],
[1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]))
#be carefull with infinity loop
print(checkio([
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],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