Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
BFS solution in Uncategorized category for Express Delivery by htamas
def checkio(map):
h, w = len(map), len(map[0])
s = next((i, j, 2) for i in range(h) for j in range(w)
if map[i][j] == 'S')
stack = [s + ('',)]
visited = {s}
while stack:
i, j, b, p = stack.pop(0)
if b == 2:
steps = [(i, j, 1, '')]
else:
steps = [(i-1, j, 2*b, 'U'), (i, j+1, 2*b, 'R'),
(i+1, j, 2*b, 'D'), (i, j-1, 2*b, 'L')]
if map[i][j] == 'B':
steps.append((i, j, 2-b, 'B'))
for s in steps:
if s[:3] not in visited:
i, j, b, d = s
if 0 <= i < h and 0 <= j < w:
if map[i][j] != 'W':
stack.append((i, j, b, p + d))
visited.add((i, j, b))
if map[i][j] == 'E' and b == 2:
return p + d
# Sent from my mobile
Jan. 14, 2014
Comments: