Problem:
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

8 Queens Problem

The goal of 8 Queens Problem is to put eight queens on a chess-board such that none of them threatens any of others. A queen threatens the squares in the same row, in the same column, or on the same diagonals as shown in the following figure.

For a given chess board where $k$ queens are already placed, find the solution of the 8 queens problem.

Input

In the first line, an integer $k$ is given. In the following $k$ lines, each square where a queen is already placed is given by two integers $r$ and $c$. $r$ and $c$ respectively denotes the row number and the column number. The row/column numbers start with 0.

Output

Print a $8 \times 8$ chess board by strings where a square with a queen is represented by 'Q' and an empty square is represented by '.'.

Constraints

There is exactly one solution

Sample Input 1

2
2 2
5 3

Sample Output 1

......Q.
Q.......
..Q.....
.......Q
.....Q..
...Q....
.Q......
....Q...

Solution in Haskell (judge verdict: Accepted)

import Data.List
import Debug.Trace
import Data.Monoid

data Status = Safe | Danger | Queen deriving (Show, Eq)
type Position = (Int, Int)
type Cell = (Position, Status)
type Board = [Cell]

-- Prepare empty board
emptyBoard :: Board
emptyBoard = sortBy xtoy [((x,y), Safe) | x <- [0..7], y <- [0..7]]
  where xtoy ((x1, y1), _) ((x2, y2), _) = mappend (compare y1 y2) (compare x1 x2)


-- Board functions
getStatus :: Cell -> Status
getStatus (position, status) = status

getPosition :: Cell -> Position
getPosition (position, status) = position

setStatus :: Status -> Cell -> Board -> Board
setStatus status (position, _) board = former ++ [(position, status)] ++ latter
  where (former, target:latter) = break ((==) position . getPosition) board

getQueens :: Board -> [Position]
getQueens board = map fst $ filter ((==) Queen . getStatus) board

getSafes :: Board -> [Position]
getSafes board = map fst $ filter ((==) Safe . getStatus) board

findDangers :: Position -> Board -> [Cell]
findDangers (px,py) board = dangerRow ++ dangerCol ++ dangerSP ++ dangerSM
  where dangerRow = filter ((==) px . fst . getPosition) board
        dangerCol = filter ((==) py . snd . getPosition) board
        dangerSP  = filter (flip elem sp . getPosition) board
        dangerSM  = filter (flip elem sm . getPosition) board
        sp        = [(x,y) | x <- [0..7], y <- [0..7], y == -x+(py+px)]
        sm        = [(x,y) | x <- [0..7], y <- [0..7], y == x+(py-px)]

putQueen :: Position -> Board -> Board
putQueen position board = setStatus Queen (position, Safe) updatedBoard
  where updatedBoard    = foldr (setStatus Danger) board (findDangers position board)


-- Queen problem solver
solveQueens :: Int -> Board -> [Board]
solveQueens 0 board = [board]
solveQueens n board = do
  position <- getSafes board
  let newBoard = putQueen position board
  solveQueens (n-1) newBoard

format :: Board -> [String]
format []    = []
format board = let (row, remain) = splitAt 8 board
                   line          = concat . map (\x -> if getStatus x == Queen then "Q" else ".") $ row
               in line : (format remain)

main = do
  n <- fmap read getLine
  inputs <- fmap (map words . lines) getContents
  let positions = map (\[y, x] -> (read x, read y)) inputs
  let initialBoard = foldr putQueen emptyBoard positions
  mapM_ putStrLn . format . head . solveQueens (8-n) $ initialBoard