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.IO;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Numerics;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using static System.Math;
using Debug = System.Diagnostics.Debug;

class P
{
    static void Main()
    {
        int n = int.Parse(Console.ReadLine());
        char[][] map = Enumerable.Repeat(0, 8).Select(_ => Enumerable.Repeat('.', 8).ToArray()).ToArray();
        bool[] hContains = new bool[8];
        bool[] wContains = new bool[8];
        bool[] hpwContains = new bool[15];
        bool[] hmwp7Contains = new bool[15];
        for (int i = 0; i < n; i++)
        {
            var rc = Console.ReadLine().Split().Select(int.Parse).ToArray();
            var r = rc[0];
            var c = rc[1];
            map[r][c] = 'Q';
            hContains[r] = true;
            wContains[c] = true;
            hpwContains[r + c] = true;
            hmwp7Contains[r - c + 7] = true;
        }
        if (!IsPossible(map, hContains, wContains, hpwContains, hmwp7Contains)) goto end;
        Console.WriteLine(string.Join("\n", res.Select(x => string.Join("", x))));
        return;
        end:;
        Console.WriteLine("No Answer");
    }
    static char[][] res = null;
    static bool IsPossible(char[][] c, bool[] hContains, bool[] wContains, bool[] hpwContains, bool[] hmwp7Contains)
    {
        if (hContains.All(x => x) && wContains.All(x => x))
        {
            res = c;
            return true;
        }
        int hindex = -1;
        for (int i = 0; i < 8; i++)
        {
            if (!hContains[i])
            {
                hindex = i;
                break;
            }
        }
        if (hindex == -1) return false;
        for (int i = 0; i < 8; i++)
        {
            if (!wContains[i] && !hpwContains[hindex + i] && !hmwp7Contains[hindex - i + 7])
            {
                var newc = c.Select(x => x.ToArray()).ToArray();
                var newhc = hContains.ToArray();
                var newwc = wContains.ToArray();
                var newhpwc = hpwContains.ToArray();
                var newhmwp7c = hmwp7Contains.ToArray();
                newc[hindex][i] = 'Q';
                newhc[hindex] = true;
                newwc[i] = true;
                newhpwc[hindex + i] = true;
                newhmwp7c[hindex - i + 7] = true;
                if (IsPossible(newc, newhc, newwc, newhpwc, newhmwp7c)) return true;
            }
        }
        return false;
    }
}