Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
9-liner: quasi heapq solution in Creative category for Express Delivery by przemyslaw.daniel
def checkio(m): # example of simulated heapq
s, v = [(0, "", True, divmod(''.join(m).find('S'), len(m)))], set()
for t, p, d, (x, y) in (s.pop(s.index(min(s))) for _ in iter(int, 1)):
if m[x][y] == 'W' or (x, y, d) in v: continue
if m[x][y] == 'E' and d: return p
v, f = v | {(x, y, d)}, lambda a, b: 0 <= a < len(m) and 0 <= b < len(m[0])
if m[x][y] == 'B': s += [(t + 1, p + 'B', not d, (x, y))]
for i, j, k in [(1, 0, 'D'), (-1, 0, 'U'), (0, 1, 'R'), (0, -1, 'L')]:
s += [(t+1+d, p+k, d, (x+i, y+j))]*f(x+i, y+j)
May 25, 2017
Comments: