-- file:8q.hs
-- filter Line at x for current available queen
fL :: Int -> [(Int,Int)] -> [(Int,Int)]
fL i l =
filter (\(x,y) -> x == i) l
-- filter blank points for next iteration
fA :: (Int,Int) -> [(Int,Int)] -> [(Int,Int)]
fA (i,j) list =
filter
(\(x,y) -> (x /= i) && (y /= j) && ((abs (x-i)) /= (abs (y-j))))
list
-- make a [(1,1)..(n,n)] full chess map
initList :: Int -> [(Int,Int)]
initList n = [(x,y) | x <- [1..n], y <- [1..n]]
-- iteration for results
nqueenIter :: Int -> Int -> [(Int,Int)] -> [[(Int,Int)]]
nqueenIter i n bl =
if i == n then
map (\x -> [x]) bl
else
concatMap
(\q -> map (q:) (nqueenIter (i+1) n (fA q bl)))
(fL i bl)
-- main function for a n-queen solution
nqueen :: Int -> [[(Int,Int)]]
nqueen n = nqueenIter 1 n (initList n)
阅读(1187) | 评论(0) | 转发(0) |