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)

#include <stdio.h>

int judge1(int[]);
void judge2(int[]);
void set(int[], int);
void print(int[]);

int n, Q[8][2];

int main(){
  int i, queen[8];

  scanf("%d",&n);
  for(i = 0; i < n; i++){
    scanf("%d%d",&Q[i][0],&Q[i][1]);
  }
  
  set(queen, 0);
}

int judge1(int queen[8]){
  int i, j;
    
  for (i = 0; i < 7; i++) {
      for (j = i+1; j < 8; j++) {
	if (queen[i] == queen[j] || abs(queen[i] - queen[j]) == j - i) return 0;
      }
  }
  return 1;
}

void judge2(int queen[8]){
  int i, j, count = 0;  

  for (i = 0; i < 8; i++){
    for(j = 0; j < n; j++){
      if(i == Q[j][0] && queen[i] == Q[j][1])count++;
    }
  }
  if(count == n) print(queen);
}

void set(int queen[], int i){
  int j;
    
  if (i == 8) {
    if (judge1(queen)) judge2(queen);
    return;
  }
    
  for (j = 0; j < 8; j++) {
    queen[i] = j;
	set(queen, i+1);
  }
}

void print(int queen[8]){
  int i, j;
  
  for(i = 0; i < 8; i++){
    for(j = 0; j < 8; j++){
      if(j == queen[i]) printf("Q");
      else printf(".");
    }
    printf("\n");
  }
}