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)

object Main {
  import scala.io.StdIn.readLine
  def main(args: Array[String]):Unit =  {
    val n: Int = readLine().toInt
    val list: List[Cell] = for (_ <- (1 to n).toList) yield {
      val Array(y, x) = readLine().split(" ").map(_.toInt)
      new Cell(x, y)
    }
    val res = search(list, 8 - n).get
    print(format(sort(res)))
  }
  def format(list:List[Cell]):String = {
    list match{
      case Nil => ""
      case (cell::cr) =>
        ("." * 8 + "\n").patch(cell.x, "Q", 1) + format(cr)
    }
  }
  def simpleFormat(list:List[Cell]):String = {
    list match{
      case Nil => ""
      case h::tail =>
        h.x.toString() + "," + h.y.toString() + "\n" + simpleFormat(tail)
    }
  }
  def include(list:List[Cell], x:Int):Boolean = {
    list match{
      case Nil => false
      case (h::tail) => h.is_on_same_line(x) || include(tail, x)
    }
  }
  def include(list:List[Cell], cell:Cell):Boolean = {
    list match{
      case Nil => false
      case (h::tail) => h == cell || include(tail, cell)
    }
  }
  def find[a](f:a => Boolean, list:List[a]):Option[a] = {
    list match{
      case Nil => None
      case (h::tail) =>
        f(h) match{
          case true => Some(h)
          case _ => find(f, tail)
        }
    }
  }
  def filter[a](f:a => Boolean, list:List[a]):List[a] = {
    list match{
      case Nil => Nil
      case (h::tail) =>
        f(h) match{
          case true => h::filter(f, tail)
          case _ => filter(f, tail)
        }
    }
  }
  def map[a, b](f:a => b, list:List[a]):List[b] = {
    list match{
      case Nil => Nil
      case (h::tail) => f(h)::map(f, tail)
    }
  }
  def findSome[a](list:List[Option[a]]):Option[a] = {
    list match{
      case Nil => None
      case (h::tail) =>
        h match{
          case Some(_) => h
          case None => findSome(tail)
        }
    }
  }
  def search(list:List[Cell], num:Int):Option[List[Cell]] = {
    num match{
      case 0 => Some(list)
      case _ =>
        val culm = find(!include(list, _:Int), (0 to 7).toList)
        culm match{
          case None => None
          case Some(x) =>
            val suspect = filter(!include(list, _:Cell), map(new Cell(x, _:Int), (0 to 7).toList))
            val res = map((cell:Cell) => search(cell::list, num - 1), suspect)
            findSome(res)
        }
    }
  }
  def length[a](list:List[a]):Int = {
    list match{
      case Nil => 0
      case _::tail => 1 + length(tail)
    }
  }
  def sort[T <:Ordered[T]](list:List[T], length:Int = -1):List[T] = {
    val len = if (length == -1)
      list.length
    else
      length
    def merge[T <: Ordered[T]](left:List[T], right:List[T]):List[T] = {
      (left, right) match {
        case (Nil, _) => right
        case (_, Nil) => left
        case (l::lt, r::rt) =>
          (l < r) match {
            case true => l::merge(lt, right)
            case false => r::merge(left, rt)
          }
      }
    }
    list match{
      case Nil => Nil
      case a::Nil => list
      case _ =>
        val mid = len / 2
        val (left, right) = list.splitAt(mid)
        merge(sort(left, mid), sort(right, len - mid))
    }
  }
}
class Cell(val x:Int, val y:Int) extends Ordered[Cell] {
  def ==(other:Cell):Boolean = {
    return (x == other.x) || (y == other.y) || (x - other.x).abs == (y - other.y).abs
  }
  def is_on_same_line(other:Int):Boolean = {
    return (x == other)
  }
  def compare(that:Cell):Int = {
    y.compare(that.y)
  }
}