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.Linq;
using System.Collections.Generic;
class EightQueen
{
	static void Main()
	{
		var n = int.Parse(Console.ReadLine());
		var rc = Enumerable.Range(0, n)
			.Select(_ => Console.ReadLine());
		
		var board = new Board(rc);
        board.InitAttackedArea();
        
        var resultBoard = board.Explore();
        
        resultBoard.Nodes.ForEach(row => 
            Console.WriteLine(string.Join("", row.Select(x => x.QueenExists ? "Q" : ".").ToArray())));
	}
}

public class Board
{
    public Node[][] Nodes{ get; set; }
 	const int Size = 8;
   
    public Board() {}
	public Board(IEnumerable<string> rc)
	{
		var queens = rc.Select(x => x.Split())
			.Select(x => new 
            { 
                Row = int.Parse(x[0]), 
                Column = int.Parse(x[1]) 
            })
			.ToArray();
		
		Nodes = Enumerable.Range(0, Size)
			.Select(row => Enumerable
				.Range(0, Size)
				.Select(column => new Node
                { 
                    QueenExists = queens.Any(x => x.Row == row && x.Column == column) 
                })
                .ToArray())
            .ToArray();
	}
    
    // ?????????????????¢???????????¨?????¢?¨????
    public void InitAttackedArea()
    {
        Nodes.ForEach((x, row) => x.ForEach((node, column) =>
        {
            ApplyAttackedArea(row, column);
        }));
    }
    
    public void ApplyAttackedArea(int row, int column)
    {
        var node = Nodes[row][column];
        if (!node.QueenExists)
            return; 
        
        // row
        Nodes[row].ForEach(x =>             
        {
            x.IsAttackedArea = true;
        });
        
        // column
        Nodes.ForEach(rows => 
        {
            rows[column].IsAttackedArea = true;
        });
        
        // from left top to right bottom
        for (int r = row - Size, c = column - Size; r < Size && c < Size; r++, c++)
        {
            // ????????????????????????????????????????§??????????????????§??????????????????????????´???????????§???????????????
            if (r >= Size || c >= Size || r < 0 || c < 0)
                continue;
            
            Nodes[r][c].IsAttackedArea = true;
        }
        
        // from right top to left bottom
        for (int r = row - Size, c = column + Size; r < Size && c >= 0; r++, c--)
        {
            // ????????????????????????????????????????§??????????????????§??????????????????????????´???????????§???????????????
            if (r >= Size || c >= Size || r < 0 || c < 0)
                continue;
            
            Nodes[r][c].IsAttackedArea = true;
        }
    }
    
    public Board Clone()
    {  
        return new Board
        { 
            Nodes = Nodes.Select(x => 
                x.Select(y => new Node
                    { 
                        IsAttackedArea = y.IsAttackedArea, 
                        QueenExists = y.QueenExists 
                    })
                    .ToArray())
                .ToArray()
        };
    }
   
    public Board Explore()
    {
        return Enumerable.Range(0, Size)
            .Where(row => Nodes[row].All(x => !x.QueenExists))
            .Select(row => Enumerable.Range(0, Size)
                .Select(column => Explore(this, row, column))
                .FirstOrDefault(x => x != null))
            .DefaultIfEmpty(this)
            .FirstOrDefault();
    }
    
    private static Board Explore(Board board, int r, int c)
    {
        if (board.Nodes[r][c].IsAttackedArea)
            return null;
        
        // check???????????????
        board.Nodes[r][c].IsAttackedArea = true;
        
        // ?????????????????£?????¨?????¨?????????????????????????????????????????????
        var b = board.Clone();
        
        b.Nodes[r][c].QueenExists = true;
        
        // ???????¨??????????queen?????¢???????????¨?????¢?¨????
        b.ApplyAttackedArea(r, c);
        
        var nextRow = Enumerable.Range(r + 1, Size - (r + 1))
            .Where(x => b.Nodes[x].All(node => !node.QueenExists))
            .DefaultIfEmpty(Size)
            .Min();
        
        // ?¬????????????£????????¢?´¢??????????????????board???????????§??°??£??????
        if (nextRow >= Size)
            return b;
        
        return Enumerable.Range(0, Size)
            .Where(x => !b.Nodes[nextRow][x].IsAttackedArea)
            .Select(x => Explore(b, nextRow, x))
            .FirstOrDefault(x => x != null);
    }
}

public class Node
{
    public bool QueenExists { get; set; }
    public bool IsAttackedArea { get; set; }
}


// ??????????????¨?????????????????????????????§???
public static class MyExtensions
{
	public static void ForEach<T>(this IEnumerable<T> e, Action<T> action)
	{
		foreach (var element in e) action(element);
	}
	
	public static void ForEach<T>(this IEnumerable<T> e, Action<T, int> action)
	{
		var i = 0;
		foreach (var element in e) action(element, i++);
	}
}