N-queens problem: Difference between revisions
Content deleted Content added
m →{{header|zkl}}: re-format |
→In terms of foldr: (Tidying) |
||
Line 5,227:
{{Trans|JavaScript}}
<lang haskell>import Data.List (transpose, intercalate)
import Data.Bool (bool)
queenPuzzle :: Int -> Int -> [[Int]]
Line 5,237 ⟶ 5,238:
foldr
(\iCol b ->
(safe (nRows - 1) iCol solution))
[]
[1 .. nCols])
Line 5,246 ⟶ 5,248:
where
safe iRow iCol solution =
zipWith
(\sc sr ->
(iCol == sc) ||
(sc + sr == (iCol + iRow)) || (sc - sr == (iCol - iRow)))
solution
[0 .. iRow - 1]
Line 5,256 ⟶ 5,259:
-- 10 columns of solutions for the 7*7 board:
showSolutions :: Int -> Int -> [String]
showSolutions nCols
unlines <$>
((fmap (intercalate " "
chunksOf nCols (queenPuzzle
where
boardLines rows =
(\r -> (bool '.' '♛' . (== r)) <$> [1 .. (length rows)]) <$> rows▼
▲ [1 .. (length rows)]) <$>
chunksOf :: Int -> [a] -> [[a]]
Line 5,312 ⟶ 5,307:
..♛.... ..♛.... ...♛... ..♛.... ♛...... .....♛. ...♛... .♛..... ......♛ ....♛..
.....♛. .....♛. .....♛. .....♛. ....♛.. ...♛... .....♛. ....♛.. ...♛... ..♛....</pre>
===Breadth-first search and Depth-first search===
|