RosettaCodeData/Task/N-queens-problem/Python/n-queens-problem-6.py

11 lines
317 B
Python

def queens(x, i, a, b, c):
if a: # a is not empty
for j in a:
if i + j not in b and i - j not in c:
yield from queens(x + [j], i + 1, a - {j}, b | {i + j}, c | {i - j})
else:
yield x
for solution in queens([], 0, set(range(8)), set(), set()):
print(solution)