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

namespace ALDS1_13A
{
    public class Program
    {
        const int Size = 8;
        readonly int[,] attack = new int[Size + 2, Size + 2];
        readonly bool[,] queen = new bool[Size + 2, Size + 2];
        readonly List<string> ansList = new List<string>();

        void Solve(StreamScanner ss, StreamWriter sw)
        {
            //---------------------------------
            var K = ss.Next(int.Parse);
            for (var i = 0; i < K; i++)
            {
                var R = ss.Next(int.Parse) + 1;
                var C = ss.Next(int.Parse) + 1;
                Put(C, R);
            }

            DFS(K, 0);
            foreach (var ans in ansList) sw.Write(ans);
            //---------------------------------
        }

        string MakeAnser()
        {
            var sb = new StringBuilder();
            for (var i = 1; i <= Size; i++)
            {
                for (var j = 1; j <= Size; j++) sb.Append(queen[i, j] ? "Q" : ".");
                sb.AppendLine();
            }
            return sb.ToString();
        }

        void DFS(int count, int pos)
        {
            if (count == Size)
            {
                ansList.Add(MakeAnser());
                return;
            }

            if (pos >= Size * Size) return;

            for (var i = pos; i < Size * Size; i++)
            {
                var x = i % Size + 1;
                var y = i / Size + 1;
                if (!CanPut(x, y)) continue;

                Put(x, y);
                DFS(count + 1, i + 1);
                Remove(x, y);
            }
        }

        bool CanPut(int x, int y)
        {
            return attack[y, x] == 0 && !queen[y, x];
        }

        void Put(int x, int y)
        {
            queen[y, x] = true;
            ChangeAttack(x, y, 1);
        }

        void Remove(int x, int y)
        {
            queen[y, x] = false;
            ChangeAttack(x, y, -1);
        }

        void ChangeAttack(int x, int y, int atk)
        {
            for (var i = 1; i <= Size; i++)
            for (var j = 1; j <= Size; j++)
            {
                if (x == j || y == i || Math.Abs(x - j) == Math.Abs(y - i)) attack[i, j] += atk;
            }
        }

        static void Main()
        {
            var ss = new StreamScanner(new StreamReader(Console.OpenStandardInput()));
            var sw = new StreamWriter(Console.OpenStandardOutput()) {AutoFlush = false};
            new Program().Solve(ss, sw);
            sw.Flush();
        }
    }

    public class StreamScanner
    {
        static readonly char[] Sep = {' '};
        readonly Queue<string> buffer = new Queue<string>();
        readonly TextReader textReader;

        public StreamScanner(TextReader textReader)
        {
            this.textReader = textReader;
        }

        public T Next<T>(Func<string, T> parser)
        {
            if (buffer.Count != 0) return parser(buffer.Dequeue());
            var nextStrings = textReader.ReadLine().Split(Sep, StringSplitOptions.RemoveEmptyEntries);
            foreach (var nextString in nextStrings) buffer.Enqueue(nextString);
            return Next(parser);
        }

        public T[] Next<T>(Func<string, T> parser, int x)
        {
            var ret = new T[x];
            for (var i = 0; i < x; ++i) ret[i] = Next(parser);
            return ret;
        }

        public T[][] Next<T>(Func<string, T> parser, int x, int y)
        {
            var ret = new T[y][];
            for (var i = 0; i < y; ++i) ret[i] = Next(parser, x);
            return ret;
        }
    }
}