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 y.miyoshi
{

    public class Queen8
    {
        static int Max = 8;
        static bool[] vertical;
        static bool[] diagonal1;
        static bool[] diagonal2;
        static Dictionary<byte, byte> queenDic;
        static byte[] skip;
        static bool finish;

        public static void Main()
        {
            var n = byte.Parse(Console.ReadLine());
            skip = new byte[n];
            queenDic = Enumerable.Range(0, n).Select(_ =>
            {
                var line = Console.ReadLine().Split();
                skip[_] = byte.Parse(line[0]);
                return new { x = byte.Parse(line[0]), y = byte.Parse(line[1])};
            }).ToDictionary(e => e.x, e=>e.y);

            Initialize();
            queenDic.ToList().ForEach(q => MarkBoad(q.Key, q.Value, true));

            Search8Queens(0);

        }

        public static void Search8Queens(byte x)
        {
            if (finish) return;

            if (x == Max)
            {
                Display();
                finish = true;
                return;
            }

            if (!skip.Contains(x))
            {
                for (byte i = 0; i < Max; i++)
                {
                    //????????????????
                    if (queenDic.ContainsKey(x)
                        || vertical[i] == true
                        || diagonal1[x + i] == true
                        || diagonal2[x - i + Max - 1] == true)
                        continue;

                    //??????
                    MarkBoad(x, i, true);
                    Search8Queens((byte)(x + 1));
                    MarkBoad(x, i, false);
                }
            }
            else
            {
                Search8Queens((byte)(x + 1));
            }

        }

        public static void Display()
        {
            for (byte i = 0; i < Max; i++)
            {
                for (byte j = 0; j < Max; j++)
                {
                    if (queenDic[i] == j)
                        Console.Write('Q');
                    else
                        Console.Write('.');
                }
                Console.WriteLine();
            }
        }

        public static void MarkBoad(byte x, byte y, bool mark)
        {
            vertical[y] = mark;
            diagonal1[x + y] = mark;
            diagonal2[x - y + Max - 1] = mark;
            if (mark) queenDic[x] = y;
            else queenDic.Remove(x);
        }

        public static void Initialize()
        {
            vertical = new bool[Max];
            diagonal1 = new bool[2 * Max - 1];
            diagonal2 = new bool[2 * Max - 1];
        }

        //public struct Queen
        //{
        //    public byte x;
        //    public byte y;
        //}
    }
}