fork(2) download
  1. import System.Environment (getArgs)
  2.  
  3. data Board = Board {
  4. field :: [Int],
  5. size :: Int
  6. }
  7.  
  8. -- | Checks whether the board has a conflict
  9. conflict :: Board -> Bool
  10. conflict b = any (==x) xs || step (-1) x xs || step 1 x xs
  11. where (x:xs) = field b
  12. step _ _ [] = False
  13. step s p (x:xs)
  14. | (s+p) == x = True
  15. | otherwise = step (s+p) p xs
  16.  
  17. -- | Returns all boards one can get by placing a queen in the next column
  18. place :: Board -> [Board]
  19. place b = place' b 1
  20. where place' b s
  21. | s > size b = []
  22. | conflict newboard = rest
  23. | otherwise = newboard:rest
  24. where newboard = Board newfield (size b)
  25. newfield = s:(field b)
  26. rest = place' b (s+1)
  27.  
  28. -- | Returns all boards one can get for the n-th queen problem
  29. placeAll :: Int -> [Board]
  30. placeAll n = foldl (>>=) [Board [] n] (replicate n place)
  31.  
  32. -- | Creates a String from a board
  33. prettyPrint :: Board -> String
  34. prettyPrint b = unlines.map prettyLine.field$b
  35. where prettyLine n = replicate (n - 1) '.' ++ "@" ++ replicate ((size b) - n) '.'
  36.  
  37. -- | Shows the first two solutions obtained by placeAll
  38. main = do
  39. mapM_ putStrLn . map prettyPrint . take 2 . placeAll $ 44
  40.  
Success #stdin #stdout 0s 6264KB
stdin
Standard input is empty
stdout
..........................................@.
........................................@...
...........................................@
.........................................@..
......................................@.....
....................................@.......
.......................................@....
.....................................@......
...................................@........
.................................@..........
...............................@............
..................................@.........
................................@...........
..............................@.............
............................@...............
..........................@.................
.............................@..............
...........................@................
.........................@..................
.......................@....................
.....................@......................
........................@...................
......................@.....................
....................@.......................
..................@.........................
................@...........................
...................@........................
.................@..........................
...............@............................
.............@..............................
...........@................................
..............@.............................
............@...............................
..........@.................................
........@...................................
.@..........................................
.........@..................................
.......@....................................
.....@......................................
...@........................................
......@.....................................
....@.......................................
..@.........................................
@...........................................

.........................................@..
...........................................@
........................................@...
..........................................@.
......................................@.....
....................................@.......
.......................................@....
.....................................@......
...................................@........
.................................@..........
...............................@............
..................................@.........
................................@...........
..............................@.............
............................@...............
..........................@.................
.............................@..............
...........................@................
.........................@..................
.......................@....................
.....................@......................
........................@...................
......................@.....................
....................@.......................
..................@.........................
................@...........................
...................@........................
.................@..........................
...............@............................
.............@..............................
...........@................................
..............@.............................
............@...............................
..........@.................................
........@...................................
.@..........................................
.........@..................................
.......@....................................
.....@......................................
...@........................................
......@.....................................
....@.......................................
..@.........................................
@...........................................