Untitled
5 years ago by PastaLord in Haskell
queens n = queens' n n
queens' n 0 = [[]]
queens' n k = [x:xs | xs <- queens' n (k - 1), x <- [1..n], isSafeColumn x xs, isSafeDiagonal x xs]
isSafeColumn x xs = not $ elem x xs
isSafeDiagonal x xs = all (\(a, b) -> abs(x - a) /= b) $ zip xs [1..]
showLine n k = replicate (k - 1) '.' ++ "X" ++ replicate (n - k) '.'
showSolution s = (mapM_ putStrLn [showLine (length s) k | k <- s]) >> putStrLn ""
main = mapM_ showSolution $ queens 4