Family Day update
This commit is contained in:
parent
aac6731f2c
commit
9ad63ea473
2442 changed files with 39761 additions and 8255 deletions
|
|
@ -5,41 +5,37 @@ queenPuzzle :: Int -> Int -> [[Int]]
|
|||
queenPuzzle nRows nCols
|
||||
| nRows <= 0 = [[]]
|
||||
| otherwise =
|
||||
foldr
|
||||
(\qs a ->
|
||||
a ++
|
||||
foldr
|
||||
(\iCol b -> bool b (b ++ [qs ++ [iCol]]) (safe (nRows - 1) iCol qs))
|
||||
[]
|
||||
[1 .. nCols])
|
||||
[]
|
||||
(queenPuzzle (nRows - 1) nCols)
|
||||
foldr (\qs a ->
|
||||
a ++ foldr (\iCol b -> bool b (b ++ [qs ++ [iCol]])
|
||||
(safe (nRows - 1) iCol qs))
|
||||
[]
|
||||
[1 .. nCols])
|
||||
[]
|
||||
(queenPuzzle (nRows - 1) nCols)
|
||||
|
||||
safe :: Int -> Int -> [Int] -> Bool
|
||||
safe iRow iCol qs =
|
||||
(not . or) $
|
||||
zipWith
|
||||
(\sc sr ->
|
||||
(iCol == sc) || (sc + sr == (iCol + iRow)) || (sc - sr == (iCol - iRow)))
|
||||
qs
|
||||
[0 .. iRow - 1]
|
||||
(not . or) $ zipWith (\sc sr -> (iCol == sc) ||
|
||||
(sc + sr == (iCol + iRow)) ||
|
||||
(sc - sr == (iCol - iRow)))
|
||||
qs
|
||||
[0 .. iRow - 1]
|
||||
|
||||
-- TEST ---------------------------------------------------
|
||||
-- 10 columns of solutions for the 7*7 board:
|
||||
showSolutions :: Int -> Int -> [String]
|
||||
showSolutions nCols nSize =
|
||||
unlines <$>
|
||||
((fmap (intercalate " ") . transpose . fmap boardLines) <$>
|
||||
chunksOf nCols (queenPuzzle nSize nSize))
|
||||
map (unlines . map (intercalate " ") . transpose . map boardLines)
|
||||
$ chunksOf nCols (queenPuzzle nSize nSize)
|
||||
where
|
||||
boardLines rows =
|
||||
(\r -> (bool '.' '♛' . (== r)) <$> [1 .. (length rows)]) <$> rows
|
||||
[map (bool '.' '♛' . (== r)) [1 .. (length rows)] | r <- rows]
|
||||
|
||||
chunksOf :: Int -> [a] -> [[a]]
|
||||
chunksOf i xs = take i <$> ($ (:)) (splits xs) []
|
||||
chunksOf i xs = splits xs
|
||||
where
|
||||
splits [] _ n = []
|
||||
splits l c n = l `c` splits (drop i l) c n
|
||||
splits [] = []
|
||||
splits l = (take i l) : splits (drop i l)
|
||||
|
||||
main :: IO ()
|
||||
main = (putStrLn . unlines) $ showSolutions 10 7
|
||||
|
|
|
|||
|
|
@ -40,21 +40,21 @@ emptySt = ([],[])
|
|||
|
||||
-- | Breadth-first search
|
||||
bfs :: Int -> [(State, Thread)] -> (State, Thread)
|
||||
bfs n [] = error "Couldn't find a feasible solution"
|
||||
bfs n [] = error "Could not find a feasible solution"
|
||||
bfs n sts | (not.empty) goal = head goal
|
||||
| otherwise = bfs n sts'
|
||||
| otherwise = bfs n sts2
|
||||
where
|
||||
goal = filter (isGoal n) sts'
|
||||
sts' = filter (feasible n) $ (moves n) <*> sts
|
||||
goal = filter (isGoal n) sts2
|
||||
sts2 = filter (feasible n) $ (moves n) <*> sts
|
||||
|
||||
-- | Depth-first search
|
||||
dfs :: Int -> (State, Thread) -> [(State, Thread)]
|
||||
dfs n st | isGoal n st = [st]
|
||||
| infeasible n st = [emptySt]
|
||||
| otherwise = do x <- [1..n]
|
||||
st' <- dfs n $ move x st
|
||||
guard $ st' /= emptySt
|
||||
return st'
|
||||
st2 <- dfs n $ move x st
|
||||
guard $ st2 /= emptySt
|
||||
return st2
|
||||
|
||||
main = do
|
||||
[narg] <- getArgs
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue