N-queens problem: Difference between revisions
Content added Content deleted
m (→Python: Niklaus Wirth algorithm: The explicit copying of the set a is made implicit again.) |
m (→Python: Niklaus Wirth algorithm: Code made compact) |
||
Line 12,848: | Line 12,848: | ||
for j in a: |
for j in a: |
||
if i + j not in b and i - j not in c: |
if i + j not in b and i - j not in c: |
||
b.add(i + j) |
b.add(i + j) ; c.add(i - j) ; x.append(j) |
||
c.add(i - j) |
|||
x.append(j) |
|||
yield from queen(i + 1, a - {j}) |
yield from queen(i + 1, a - {j}) |
||
b.remove(i + j) |
b.remove(i + j) ; c.remove(i - j) ; x.pop() |
||
c.remove(i - j) |
|||
x.pop() |
|||
else: |
else: |
||
yield x |
yield x |
||
b = set() |
b = set() ; c = set() ; x = [] |
||
c = set() |
|||
x = [] |
|||
yield from queen(0, set(range(n))) |
yield from queen(0, set(range(n))) |
||