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

import scala.collection.mutable
import scala.io.StdIn._

sealed abstract class Piece
case object Queen extends Piece
case object Danger extends Piece
case object Empty extends Piece

object Main extends App {
  type Pos = mutable.Seq[mutable.Seq[Piece]]
  val k = readInt()
  val pos: Pos = mutable.Seq.fill(8, 8)(Empty)

  val next = List(
    (r: Int, c: Int) => (r - 1, c),
    (r: Int, c: Int) => (r + 1, c),
    (r: Int, c: Int) => (r, c - 1),
    (r: Int, c: Int) => (r, c + 1),
    (r: Int, c: Int) => (r - 1, c - 1),
    (r: Int, c: Int) => (r - 1, c + 1),
    (r: Int, c: Int) => (r + 1, c - 1),
    (r: Int, c: Int) => (r + 1, c + 1)
  )

  for (i <- 1 to k) {
    val Array(r, c) = readLine().split(" ").map(_.toInt)
    pos(r)(c) = Queen
    next.foreach(f => {
      var row = r
      var col = c
      while (isOnBoard(row, col)) {
        if (pos(row)(col) == Empty) pos(row)(col) = Danger
        val n = f(row, col)
        row = n._1
        col = n._2
      }
    })
  }

  solve(0)

  pos.foreach(r => {
    r.foreach {
      case Queen => print("Q")
      case _ => print(".")
    }
    println
  })

  def isOnBoard(r: Int, c: Int): Boolean = 0 <= r && r <= 7 && 0 <= c && c <= 7

  def isSafe(r: Int, c: Int): Boolean = {
    next foreach (f => {
      var row = r
      var col = c
      while (isOnBoard(row, col)) {
        if (pos(row)(col) == Queen) return false
        val n = f(row, col)
        row = n._1
        col = n._2
      }
    })
    true
  }

  def solve(r: Int): Boolean = {
    if (pos(r) contains Queen) {
      if (r == 7) return true
      else return solve(r + 1)
    }

    for (c <- pos(r).indices) {
      if (pos(r)(c) == Empty && isSafe(r, c)) {
        pos(r)(c) = Queen
        if (r == 7 || solve(r + 1)) return true
        else pos(r)(c) = Empty
      }
    }
    false
  }
}