Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import itertools
- deltas = [(x*a,y*b) for x,y in itertools.permutations((1,2)) for a,b in itertools.product((-1,1),repeat=2)]
- valid = {(i,j) for i in range(8) for j in range(8)}
- def knight(curpos=(0,0), seen=None):
- if seen is None: seen = {curpos}
- if len(seen) == len(valid):
- yield (curpos,)
- return
- x,y = curpos
- for dx, dy in deltas:
- candidate_pos = x+dx, y+dy
- if candidate_pos in valid and candidate_pos not in seen:
- seen.add(candidate_pos)
- for result in knight(candidate_pos, seen):
- seen.remove(candidate_pos)
- yield (curpos,) + result
- seen.add(candidate_pos)
- seen.remove(candidate_pos)
- for solution in knight():
- print(solution)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement