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 ALDS1_13_A
{
    class Program
    {
        static void Main(string[] args)
        {
            int k = int.Parse(Console.ReadLine());

            List<int> firstx = new List<int>();
            List<int> firsty = new List<int>();
            for (int i = 0; i < 8; i++)
            {
                firstx.Add(i);
                firsty.Add(i);
            }

            int[,] ans = new int[8, 8];
            for (int i = 0; i < k; i++)
            {
                var input = ReadInt();
                ans[input[0], input[1]] = 1;
                firstx.Remove(input[0]);
                firsty.Remove(input[1]);
            }

            var searchX = GetPermutation(firstx).ToArray();
            var searchY = GetPermutation(firsty).ToArray();
            var max = searchX.GetLength(0);

            for (int i = 0; i < max; i++)
            {
                bool ok = false;
                for (int j = 0; j < max; j++)
                {
                    int[,] temp = new int[8, 8];
                    Array.Copy(ans, temp, 64);

                    for (int m = 0; m < 8 - k; m++)
                    {
                        int indexx = searchX[i][m];
                        int indexy = searchY[j][m];
                        temp[indexx, indexy] = 1;
                    }

                    ok = CheckMap(temp);
                    if (ok)
                    {
                        ans = temp;
                        break;
                    }
                }
                if (ok) break;
            }

            for (int i = 0; i < 8; i++)
            {
                for (int j = 0; j < 8; j++)
                {
                    if (ans[i, j] == 1) Console.Write("Q");
                    else Console.Write(".");
                }
                Console.WriteLine();
            }
            Console.ReadKey();
        }


        static bool CheckMap(int[,] map)
        {
            int[] sums = new int[15];
            int[] sums2 = new int[15];
            for (int i = 0; i < 8; i++)
            {
                for (int j = 0; j < 8; j++)
                {
                    sums[(i + j)] += map[i, j];
                    sums2[(j - i) + 7] += map[i, j];
                }
            }

            int sum = sums.Where(s => s > 1).Count();
            int sum2 = sums2.Where(s => s > 1).Count();
            if (sum != 0 || sum2 != 0) return false;
            else return true;
        }

        /// <summary>
        /// 順列組み合わせ
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="collection"></param>
        /// <returns></returns>
        public static IEnumerable<T[]> GetPermutation<T>(IEnumerable<T> collection)
        {
            if (collection.Count() == 1)
            {
                yield return new T[] { collection.First() };
            }

            foreach (var item in collection)
            {
                var selected = new T[] { item };
                var unused = collection.Except(selected);

                foreach (var rightside in GetPermutation(unused))
                {
                    yield return selected.Concat(rightside).ToArray();
                }
            }
        }


        static int[] ReadInt()
        {
            int[] ret = Console.ReadLine().Split().Select(int.Parse).ToArray();
            return ret;
        }
        static long[] ReadLong()
        {
            long[] ret = Console.ReadLine().Split().Select(long.Parse).ToArray();
            return ret;
        }
    }
}