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 OCaml (judge verdict: Accepted)

module ArrayL = ArrayLabels
module ListL = ListLabels

let dbg = Printf.printf "[debug]%s"

let max_num = 100_000_000_000

let id = fun x -> x
let tuple2 x y = (x,y)
let tuple3 x y z = (x,y,z)
let succ x = x + 1
let pred x = x - 1

let (++) n m =
  let rec aux i =
    if i = m then [m]
    else i :: aux (i+1) in
  if n > m then [] else aux n

let (++^) n m = n ++ (m-1)

let scan fmt f = Scanf.sscanf (read_line ()) fmt f

let scan_list ~sep cnv =
  let line = read_line () in
  Str.split (Str.regexp_string sep) line
  |> List.map cnv

let scan_lines n fmt f =
  List.map (fun _ -> scan fmt f) (0++^n)

let scan_matrix n m e conv =
  let arr = Array.make_matrix n m e in
  Array.iteri (fun i line ->
      let s = Scanf.Scanning.from_string @@ read_line () in
      Array.iteri (fun j _ ->
          arr.(i).(j) <- Scanf.bscanf s " %s" conv;
        ) line) arr; arr

let rec powerset = function
  | [] -> [[]]
  | hd::tl ->
    let pws =  powerset tl in
    pws @ ListL.map pws ~f:(fun pw -> hd::pw)

let permutations l =
  let rec interleave x = function
    | [] -> [[x]]
    | (hd::tl) as l ->
      (x::l) :: (interleave x tl |> ListL.map ~f:(fun l -> hd::l)) in
  let rec aux = function
    | [] -> [[]]
    | a::rest ->
      aux rest |> ListL.map ~f:(interleave a) |> List.concat in
  aux l

let between n x m = n <= x && x < m

let string_to_list s =
  List.map (String.get s) (0 ++^ String.length s)

let n = scan "%d" id
let ls = scan_lines n "%d %d" tuple2

exception Found of int array array
let print_mat mat =
  ArrayL.iter mat ~f:(fun a ->
      ArrayL.iter a ~f:(fun v ->
          if v = 0 then Printf.printf "."
          else Printf.printf "Q"
        );
      Printf.printf "\n"
    )

let () =
  let sets = permutations (0++^8) in
  (try ListL.iter sets ~f:(fun set ->
       let mat = Array.make_matrix 8 8 0 in
       ListL.iteri set ~f:(fun i n -> mat.(i).(n) <- 1;);
       if not @@ ListL.for_all ls ~f:(fun (i, j) -> mat.(i).(j) = 1) then ()
       else
       if ListL.map (0++^8) ~f:(fun i ->
           ListL.map (0++^8) ~f:(fun j ->
               if mat.(i).(j) = 1 then
                 ListL.for_all (0++^8) ~f:(fun k -> k = i || mat.(k).(j) = 0)
                 && ListL.for_all (1++^8) ~f:(fun k -> i-k < 0 || j-k < 0 || mat.(i-k).(j-k) = 0)
                 && ListL.for_all (1++^8) ~f:(fun k -> i+k >= 8 || j+k >= 8 || mat.(i+k).(j+k) = 0)
                 && ListL.for_all (1++^8) ~f:(fun k -> i-k < 0 || j+k >= 8 || mat.(i-k).(j+k) = 0)
                 && ListL.for_all (1++^8) ~f:(fun k -> i+k >= 8 || j-k < 0 || mat.(i+k).(j-k) = 0)
               else true))
          |> List.concat |> List.for_all ((=) true) then raise @@ Found mat else ());
      Array.make_matrix 8 8 0
   with
   | Found x -> x)
  |> print_mat