Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
13-liner: by stack solution in Clear category for Chess Knight by przemyslaw.daniel
STEPS = (1, 2), (2, 1), (2, -1), (1, -2), (-1, -2), (-2, -1), (-2, 1), (-1, 2)
VALID = {x+y for x in 'abcdefgh' for y in '12345678'}
def chess_knight(start, num_of_moves):
stack, result = [(num_of_moves, start)], []
while stack:
move, last = stack.pop()
result += [last]*(move < num_of_moves)
if not move: continue
x, y = map(ord, last)
new = {chr(x+i)+chr(y+j) for i, j in STEPS}
stack += [(move-1, x) for x in new & VALID]
return sorted(set(result))
May 21, 2018
Comments: