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;
using System.Threading.Tasks;

namespace lec07A
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] array = new int[8] { -1, -1, -1, -1, -1, -1, -1, -1 };

            string input = Console.ReadLine();
            //string[] split = input.Split(' ');


            int num = int.Parse(input);

            for (int ii = 0; ii < num; ii++)
            {
                input = Console.ReadLine();
                string[] split = input.Split(' ');

                int jj = int.Parse(split[0]);
                int kk = int.Parse(split[1]);

                array[jj] = kk;
               // Console.WriteLine(jj);
               
            }

            bool f = false;
            
            int i =0 , j = 0, k = 0, l = 0, m = 0, n = 0, o = 0, p = 0;
            
            for (int ci = 0; ci < 8; ci++)
            {
                if (array[0] >= 0) { i = array[0]; } else { i = ci; }
                for (int cj = 0; cj < 8; cj++)
                {
                    if (array[1] >= 0) { j = array[1]; } else {j=cj; }
                    for (int ck = 0; ck < 8; ck++)
                    {
                        if (array[2] >= 0) { k = array[2]; } else {k= ck; }
                        for (int cl = 0; cl < 8; cl++)
                        {
                            if (array[3] >= 0) { l = array[3]; } else {l= cl; }
                            for (int cm = 0; cm < 8; cm++)
                            {
                                if (array[4] >= 0) { m = array[4]; } else {m= cm; }
                                for (int cn = 0; cn < 8; cn++)
                                {
                                    if (array[5] >= 0) { n = array[5]; } else {n= cn; }
                                    for (int co = 0; co < 8; co++)
                                    {
                                        if (array[6] >= 0) { o = array[6]; } else {o= co; }
                                        for (int cp = 0; cp < 8; cp++)
                                        {
                                            if (array[7] >= 0) { p = array[7]; } else {p= cp; }
                                            f = Solve(i, j, k, l, m, n, o, p);
                                            if (f == true) { break; }
                                        }
                                        if (f == true) { break; }
                                    }
                                    if (f == true) { break; }
                                }
                                if (f == true) { break; }
                            }
                            if (f == true) { break; }
                        }
                        if (f == true) { break; }
                    }
                    if (f == true) { break; }
                }
                if (f == true) { break; }
            }
            array[0] = i;array[1] = j;array[2]=k; array[3]=l; array[4]=m; array[5]=n; array[6]=o; array[7]=p;

            for (int ii=0; ii< 8; ii++)
            {
                char[] output= new char[8] { '.', '.', '.', '.', '.' ,'.', '.', '.' };
                output[array[ii]] = 'Q';

                Console.WriteLine(output);
            }
            


            Console.ReadLine();

        }


        static bool Solve(int i, int j, int k, int l, int m, int n, int o, int p)
        {
            return (i != j) && (i != k) && (i != l) && (i != m) && (i != n) && (i != o) && (i != p)
        && (j != k) && (j != l) && (j != m) && (j != n) && (j != o) && (j != p)
        && (k != l) && (k != m) && (k != n) && (k != o) && (k != p)
        && (l != m) && (l != n) && (l != o) && (l != p)
        && (m != n) && (m != o) && (m != p)
        && (n != o) && (n != p)
        && (o != p)
        && (System.Math.Abs(i - j) != 1) && (System.Math.Abs(j - k) != 1) && (System.Math.Abs(k - l) != 1) && (System.Math.Abs(l - m) != 1)
        && (System.Math.Abs(m - n) != 1) && (System.Math.Abs(n - o) != 1) && (System.Math.Abs(o - p) != 1)
        && (System.Math.Abs(i - k) != 2) && (System.Math.Abs(j - l) != 2) && (System.Math.Abs(k - m) != 2) && (System.Math.Abs(l - n) != 2)
        && (System.Math.Abs(m - o) != 2) && (System.Math.Abs(n - p) != 2)
        && (System.Math.Abs(i - l) != 3) && (System.Math.Abs(j - m) != 3) && (System.Math.Abs(k - n) != 3) && (System.Math.Abs(l - o) != 3)
        && (System.Math.Abs(m - p) != 3)
        && (System.Math.Abs(i - m) != 4) && (System.Math.Abs(j - n) != 4) && (System.Math.Abs(k - o) != 4) && (System.Math.Abs(l - p) != 4)
        && (System.Math.Abs(i - n) != 5) && (System.Math.Abs(j - o) != 5) && (System.Math.Abs(k - p) != 5)
        && (System.Math.Abs(i - o) != 6) && (System.Math.Abs(j - p) != 6)
        && (System.Math.Abs(i - p) != 7);


        }
    }
}