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;

namespace CSharpSample01
{
    class Program
    {
        static bool[,] q;
        const int N = 8;
        const int Free = -1;
        const int Not_Free = 1;
        static int[] row;  
        static int[] culm;  
        static int[] slan_right; 
        static int[] slan_left;
        

        
       
        static void Visit(int i)
        {
            
            if(i == N)
            {
                Print();
                return;
            }
            
            for(int j = 0; j < N; j++)
            {

                if(culm[j] == Not_Free || slan_right[i + j] == Not_Free || slan_left[i - j + N - 1] == Not_Free)
                {
                    continue;
                }

                row[i] = j;  
                culm[j] = Not_Free;
                slan_right[i + j] = Not_Free;
                slan_left[i - j + N - 1] = Not_Free;

                
                Visit(i + 1);

                row[i] = Free;
                culm[j] = Free;
                slan_right[i + j] = Free;
                slan_left[i - j + N - 1] = Free;

            }

        }

        static void Print()
        {
            for(int i = 0; i < N; i++)
            {
                for(int j = 0; j < N; j++)
                {
                    if(q[i, j])
                    {
                        if(row[i] != j)
                        {
                            return;
                        }
                    }
                }
            }


            for(int i = 0; i < N; i++)
            {
                for(int j = 0; j < N; j++)
                {
                    if(row[i] == j)
                    {
                        Console.Write("Q");
                    }
                    else
                    {
                        Console.Write(".");
                    }
                }
                Console.WriteLine();
            }

        }

        static void Initial()
        {
            for(int i = 0; i < N; i++)
            {
                row[i] = Free;
                culm[i] = Free;
            }
            for(int i = 0; i < 2 * N - 1; i++)
            {
                slan_left[i] = Free;
                slan_right[i] = Free;
            }

        }


        static void Main(string[] args)
        {

            q = new bool[N, N];
            row = new int[N];
            culm = new int[N];
            slan_right = new int[2 * N - 1];
            slan_left = new int[2 * N - 1];
            
            Initial();
    
            int k = int.Parse(Console.ReadLine());

            string[] input;
            int r;
            int c;
            for(int i = 0; i < k; i++)
            {
                input = Console.ReadLine().Split(' ');
                r = int.Parse(input[0]);
                c = int.Parse(input[1]);
                q[r, c] = true;
            }

            Visit(0);            
                  
        }
    }
}