Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
The Shortest Knight's Path solution in Clear category for The Shortest Knight's Path by tssrkt777
def checkio(cells):
(start, end), steps, vd = [(8-int(e[1]), ord(e[0])-97) for e in cells.split('-')], 0, set()
STEPS, nxt = [(1, 2), (1, -2), (-1, 2), (-1, -2), (2, 1), (2, -1), (-2, 1), (-2, -1)], {start}
while end not in nxt:
vd.update(nxt)
nxt = {(x + dx, y + dy) for (x, y) in nxt
for dx, dy in STEPS if 8 > x + dx >= 0 and 8 > y + dy >= 0 and (x + dx, y + dy) not in vd}
steps += 1
return steps
if __name__ == "__main__":
#These "asserts" using only for self-checking and not necessary for auto-testing
assert checkio("b1-d5") == 2, "1st example"
assert checkio("a6-b8") == 1, "2nd example"
assert checkio("h1-g2") == 4, "3rd example"
assert checkio("h8-d7") == 3, "4th example"
assert checkio("a1-h8") == 6, "5th example"
Oct. 16, 2021
Comments: