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;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class NQueens
    {
        readonly int n;

        List<KeyValuePair<int, int>> qPos;
        int[] row;
        bool[] col, dpos, dneg;

        public NQueens(int N)
        {
            n = N;

            qPos = new List<KeyValuePair<int, int>>();
            row = new int[n];
            col = new bool[n];
            dpos = new bool[2 * n - 1];
            dneg = new bool[2 * n - 1];
        }

        public void AddQueen(int i, int j)
        {
            qPos.Add(new KeyValuePair<int, int>(i, j));
        }

        public void Solve(int i)
        {
            if (i == n)
            {
                if (CheckQueenPos()) PrintBoard();
                return;
            }

            for (int j = 0; j < n; j++)
            {
                if (!IsValid(i, j)) continue;

                ChangeBoard(i, j, true);
                Solve(i + 1);
                ChangeBoard(i, j, false);
            }
        }

        private void ChangeBoard(int i, int j, bool isSet)
        {
            row[i] = isSet ? j : -1;
            col[j] = dpos[i + j] = dneg[i - j + n - 1] = isSet;
        }

        private bool IsValid(int i, int j)
        {
            if (col[j]) return false;
            if (dpos[i + j]) return false;
            if (dneg[i - j + n - 1]) return false;
            return true;
        }

        private bool CheckQueenPos()
        {
            int m = qPos.Count;

            for (int i = 0; i < m; i++)
            {
                int index = qPos[i].Key;
                if (row[index] != qPos[i].Value) return false;
            }
            return true;
        }

        private void PrintBoard()
        {
            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    sb.Append(row[i] == j ? "Q" : ".");
                }
                sb.AppendLine();
            }
            Console.Write(sb);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            NQueens Q = new NQueens(8);

            int n = int.Parse(Console.ReadLine());

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

                Q.AddQueen(h, w);
            }

            Q.Solve(0);
        }
    }
}