September Morn Update
This commit is contained in:
parent
4e2d22a71d
commit
aac6731f2c
6856 changed files with 141342 additions and 21127 deletions
|
|
@ -1,23 +1,11 @@
|
|||
def queens(n):
|
||||
a = list(range(n))
|
||||
up = [True]*(2*n - 1)
|
||||
down = [True]*(2*n - 1)
|
||||
def sub(i):
|
||||
if i == n:
|
||||
yield tuple(a)
|
||||
else:
|
||||
for k in range(i, n):
|
||||
j = a[k]
|
||||
p = i + j
|
||||
q = i - j + n - 1
|
||||
if up[p] and down[q]:
|
||||
up[p] = down[q] = False
|
||||
a[i], a[k] = a[k], a[i]
|
||||
yield from sub(i + 1)
|
||||
up[p] = down[q] = True
|
||||
a[i], a[k] = a[k], a[i]
|
||||
yield from sub(0)
|
||||
def solve(n, i, a, b, c):
|
||||
if i < n:
|
||||
for j in range(n):
|
||||
if j not in a and i+j not in b and i-j not in c:
|
||||
for solution in solve(n, i+1, a+[j], b+[i+j], c+[i-j]):
|
||||
yield solution
|
||||
else:
|
||||
yield a
|
||||
|
||||
#Count solutions for n=8:
|
||||
sum(1 for p in queens(8))
|
||||
92
|
||||
for solution in solve(8, 0, [], [], []):
|
||||
print(solution)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue