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)
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
def queens_lex(n):
|
||||
def queens(n):
|
||||
a = list(range(n))
|
||||
up = [True]*(2*n - 1)
|
||||
down = [True]*(2*n - 1)
|
||||
|
|
@ -7,25 +7,17 @@ def queens_lex(n):
|
|||
yield tuple(a)
|
||||
else:
|
||||
for k in range(i, n):
|
||||
a[i], a[k] = a[k], a[i]
|
||||
j = a[i]
|
||||
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
|
||||
x = a[i]
|
||||
for k in range(i + 1, n):
|
||||
a[k - 1] = a[k]
|
||||
a[n - 1] = x
|
||||
a[i], a[k] = a[k], a[i]
|
||||
yield from sub(0)
|
||||
|
||||
next(queens(31))
|
||||
(0, 2, 4, 1, 3, 8, 10, 12, 14, 6, 17, 21, 26, 28, 25, 27, 24, 30, 7, 5, 29, 15, 13, 11, 9, 18, 22, 19, 23, 16, 20)
|
||||
|
||||
next(queens_lex(31))
|
||||
(0, 2, 4, 1, 3, 8, 10, 12, 14, 5, 17, 22, 25, 27, 30, 24, 26, 29, 6, 16, 28, 13, 9, 7, 19, 11, 15, 18, 21, 23, 20)
|
||||
|
||||
#Compare to A065188
|
||||
#1, 3, 5, 2, 4, 9, 11, 13, 15, 6, 8, 19, 7, 22, 10, 25, 27, 29, 31, 12, 14, 35, 37, ...
|
||||
#Count solutions for n=8:
|
||||
sum(1 for p in queens(8))
|
||||
92
|
||||
|
|
|
|||
31
Task/N-queens-problem/Python/n-queens-problem-7.py
Normal file
31
Task/N-queens-problem/Python/n-queens-problem-7.py
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
def queens_lex(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):
|
||||
a[i], a[k] = a[k], a[i]
|
||||
j = a[i]
|
||||
p = i + j
|
||||
q = i - j + n - 1
|
||||
if up[p] and down[q]:
|
||||
up[p] = down[q] = False
|
||||
yield from sub(i + 1)
|
||||
up[p] = down[q] = True
|
||||
x = a[i]
|
||||
for k in range(i + 1, n):
|
||||
a[k - 1] = a[k]
|
||||
a[n - 1] = x
|
||||
yield from sub(0)
|
||||
|
||||
next(queens(31))
|
||||
(0, 2, 4, 1, 3, 8, 10, 12, 14, 6, 17, 21, 26, 28, 25, 27, 24, 30, 7, 5, 29, 15, 13, 11, 9, 18, 22, 19, 23, 16, 20)
|
||||
|
||||
next(queens_lex(31))
|
||||
(0, 2, 4, 1, 3, 8, 10, 12, 14, 5, 17, 22, 25, 27, 30, 24, 26, 29, 6, 16, 28, 13, 9, 7, 19, 11, 15, 18, 21, 23, 20)
|
||||
|
||||
#Compare to A065188
|
||||
#1, 3, 5, 2, 4, 9, 11, 13, 15, 6, 8, 19, 7, 22, 10, 25, 27, 29, 31, 12, 14, 35, 37, ...
|
||||
145
Task/N-queens-problem/Python/n-queens-problem-8.py
Normal file
145
Task/N-queens-problem/Python/n-queens-problem-8.py
Normal file
|
|
@ -0,0 +1,145 @@
|
|||
'''N Queens problem'''
|
||||
|
||||
from functools import reduce
|
||||
from itertools import chain
|
||||
|
||||
|
||||
# queenPuzzle :: Int -> Int -> [[Int]]
|
||||
def queenPuzzle(nRows, nCols):
|
||||
'''All board patterns of this dimension
|
||||
in which no two Queens share a row,
|
||||
column, or diagonal.
|
||||
'''
|
||||
def go(nRows, nCols):
|
||||
return reduce(
|
||||
lambda a, xys: a + reduce(
|
||||
lambda b, iCol: b + [xys + [iCol]] if (
|
||||
safe(nRows - 1, iCol, xys)
|
||||
) else b,
|
||||
enumFromTo(1)(nCols),
|
||||
[]
|
||||
),
|
||||
go(nRows - 1, nCols),
|
||||
[]
|
||||
) if nRows > 0 else [[]]
|
||||
return go(nRows, nCols)
|
||||
|
||||
|
||||
# safe :: Int -> Int -> [Int] -> Bool
|
||||
def safe(iRow, iCol, pattern):
|
||||
'''True if no two queens in the pattern
|
||||
share a row, column or diagonal.
|
||||
'''
|
||||
def p(sc, sr):
|
||||
return (iCol == sc) or (
|
||||
sc + sr == (iCol + iRow)
|
||||
) or (sc - sr == (iCol - iRow))
|
||||
return not any(map(p, pattern, range(0, iRow)))
|
||||
|
||||
|
||||
# TEST ----------------------------------------------------
|
||||
# main :: IO ()
|
||||
def main():
|
||||
'''Number of solutions for boards of various sizes'''
|
||||
|
||||
n = 5
|
||||
xs = queenPuzzle(n, n)
|
||||
|
||||
print(
|
||||
str(len(xs)) + ' solutions for a {n} * {n} board:\n'.format(n=n)
|
||||
)
|
||||
print(showBoards(10)(xs))
|
||||
|
||||
print(
|
||||
fTable(
|
||||
'\n\n' + main.__doc__ + ':\n'
|
||||
)(str)(lambda n: str(n).rjust(3, ' '))(
|
||||
lambda n: len(queenPuzzle(n, n))
|
||||
)(enumFromTo(1)(10))
|
||||
)
|
||||
|
||||
|
||||
# GENERIC -------------------------------------------------
|
||||
|
||||
# enumFromTo :: (Int, Int) -> [Int]
|
||||
def enumFromTo(m):
|
||||
'''Integer enumeration from m to n.'''
|
||||
return lambda n: list(range(m, 1 + n))
|
||||
|
||||
|
||||
# chunksOf :: Int -> [a] -> [[a]]
|
||||
def chunksOf(n):
|
||||
'''A series of lists of length n, subdividing the
|
||||
contents of xs. Where the length of xs is not evenly
|
||||
divible, the final list will be shorter than n.
|
||||
'''
|
||||
return lambda xs: reduce(
|
||||
lambda a, i: a + [xs[i:n + i]],
|
||||
range(0, len(xs), n), []
|
||||
) if 0 < n else []
|
||||
|
||||
|
||||
# intercalate :: [a] -> [[a]] -> [a]
|
||||
# intercalate :: String -> [String] -> String
|
||||
def intercalate(x):
|
||||
'''The concatenation of xs
|
||||
interspersed with copies of x.
|
||||
'''
|
||||
return lambda xs: x.join(xs) if isinstance(x, str) else list(
|
||||
chain.from_iterable(
|
||||
reduce(lambda a, v: a + [x, v], xs[1:], [xs[0]])
|
||||
)
|
||||
) if xs else []
|
||||
|
||||
|
||||
# FORMATTING ----------------------------------------------
|
||||
|
||||
# showBoards :: Int -> [[Int]] -> String
|
||||
def showBoards(nCols):
|
||||
'''String representation, with N columns
|
||||
of a set of board patterns.
|
||||
'''
|
||||
def showBlock(b):
|
||||
return '\n'.join(map(intercalate(' '), zip(*b)))
|
||||
|
||||
def go(bs):
|
||||
return '\n\n'.join(map(
|
||||
showBlock,
|
||||
chunksOf(nCols)(
|
||||
list(map(showBoard, bs))
|
||||
)
|
||||
))
|
||||
return lambda boards: go(boards)
|
||||
|
||||
|
||||
# showBoard :: [Int] -> String
|
||||
def showBoard(xs):
|
||||
'''String representation of a Queens board.'''
|
||||
lng = len(xs)
|
||||
|
||||
def showLine(n):
|
||||
return ('.' * (n - 1)) + '♛' + ('.' * (lng - n))
|
||||
return list(map(showLine, xs))
|
||||
|
||||
|
||||
# fTable :: String -> (a -> String) ->
|
||||
# (b -> String) -> (a -> b) -> [a] -> String
|
||||
def fTable(s):
|
||||
'''Heading -> x display function -> fx display function ->
|
||||
f -> xs -> tabular string.
|
||||
'''
|
||||
def go(xShow, fxShow, f, xs):
|
||||
ys = [xShow(x) for x in xs]
|
||||
w = max(map(len, ys))
|
||||
return s + '\n' + '\n'.join(map(
|
||||
lambda x, y: y.rjust(w, ' ') + ' -> ' + fxShow(f(x)),
|
||||
xs, ys
|
||||
))
|
||||
return lambda xShow: lambda fxShow: lambda f: lambda xs: go(
|
||||
xShow, fxShow, f, xs
|
||||
)
|
||||
|
||||
|
||||
# MAIN ---
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Loading…
Add table
Add a link
Reference in a new issue