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

using System;

namespace A
{
    class Bord
    {
        public int[] row = new int[8];
        public bool[] col = new bool[8];
        public bool[] dpos = new bool[15];
        public bool[] dneg = new bool[15];
        public bool[] check = new bool[9];

        public void putQueen(int i, ref Bord bord)
        {
            int j, k=i+1;
            if (i == 8)
            {
                print(bord);
            }
            else
            {
                for (j = 0; j < 8; j++)
                {
                    if (bord.col[j] == false && bord.dpos[i + j] == false && bord.dneg[i - j + 7] == false)
                    {
                        bord.row[i] = j;
                        bord.col[j] = bord.dpos[i + j] = bord.dneg[i - j + 7] = true;

                        for (k = i + 1; k < 9; k++)
                        {
                            if (bord.check[k] == false)break;
                        }

                        putQueen(k, ref bord);
                        bord.col[j] = bord.dpos[i + j] = bord.dneg[i - j + 7] = false;
                    }
                }
            }
        }

        public void print(Bord bord)
        {
            for (int i = 0; i < 8; i++)
            {
                for (int j = 0; j < 8; j++)
                {
                    if (bord.row[i] == j) Console.Write("Q");
                    else Console.Write(".");
                }
                Console.WriteLine();
            }
        }
    }

    class A
    {
        static void Main(string[] args)
        {
            int k = int.Parse(Console.ReadLine()), i;
            Bord bord = new Bord();
            bord.check[8] = false;
            for (i = 0; i < 15; i++)
            {
                if (i < 8)
                {
                    bord.row[i] = -1;
                    bord.col[i] = false;
                    bord.check[i] = false;
                }
                bord.dpos[i] = false;
                bord.dneg[i] = false;
            }

            for (i = 0; i < k; i++)
            {
                string[] input = Console.ReadLine().Split(' ');
                int m = int.Parse(input[0]);
                int n = int.Parse(input[1]);

                bord.row[m] = n;
                bord.col[n] = bord.dpos[n + m] = bord.dneg[m - n + 7] = true;
                bord.check[m] = true;
            }
            int a = 0;
            for (int b = 0; b < 8; b++)
            {
                if (bord.check[a] == true) a++;
                else break;
            }
            bord.putQueen(a, ref bord);
        }
    }
}