Skip to content

Instantly share code, notes, and snippets.

@qzchenwl
Created August 6, 2011 06:41
Show Gist options
  • Save qzchenwl/1129088 to your computer and use it in GitHub Desktop.
Save qzchenwl/1129088 to your computer and use it in GitHub Desktop.
Eight queens puzzle
import Data.List
isUnique :: (Ord a) => [a] -> Bool
isUnique = all (null . drop 1) . group . sort
cols = [0..7]
solutions = [vec | vec <- permutations cols
, isUnique [vec!!i + i | i <- cols]
, isUnique [vec!!i - i | i <- cols]]
board :: [Int] -> String
board xs = unlines [replicate x '+' ++ "Q" ++ replicate (last cols - x) '+'
| x <- xs]
printSolution = putStrLn . board
printNthSolution n = printSolution (solutions !! n)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment