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: Runtime Error)

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define N 8

int *r,*c,k,a=0;

void printQueen(int queen[N])
{
  int i,j,l;
    for (i = 0; i < N; i++) {
      for(j = 0 ; j < N ; j++){
	if(j == queen[i]){
	  printf("Q");
	  continue;
	}
        printf(".");
      }
      
      printf("\n");
    }
}

/* (i,j)???????????????????¨????????????????????????? board ???????????????????´?????????? d???????????? */
void changeBoard(int board[N][N], int i, int j, int d)
{
    int k;
    
    for (k = 0; k < N; k++) { 
        board[i][k] += d;             /* ?¨??????? */
        board[k][j] += d;             /* ????????? */
    }
    if (i > j) {
        for (k = 0; k < N-(i-j); k++) {
            board[k+(i-j)][k] += d;   /* ???????????????????????? (i > j?????¨?????? */
        }
    } else {
        for (k = 0; k < N-(j-i); k++) {
            board[k][k+(j-i)] += d;   /* ???????????????????????? (i <= j?????¨?????? */
        }
    }
    if (i+j < N) {
        for (k = 0; k <= i+j; k++) {
            board[i+j-k][k] += d;     /* ???????????????????????????i +j < N?????¨?????? */
        }
    } else {
        for (k = i+j-N+1; k < N; k++) {
            board[i+j-k][k] += d;     /* ???????????????????????????i+j >= N?????¨?????? */
        }
    }
}

/* i ??????????????????????????????????¨????????????? setQueen(queen, board, i+1) ???????????????
??????????????? i == N ?????¨???????????????????§£??? queen ?????\??£??????????????§???????????¨?????????????????§?????? */
void setQueen(int queen[N], int board[N][N], int i)
{
  int j,flag = 0;


  for(j = 0 ; j < k ; j++){
    if(queen[r[j]] == c[j])flag++;
  }
  
    if (i == N && flag == k) {                              /* ?§£??????????????£??? */
        printQueen(queen);
        return;
    }
    
    for (j = 0; j < N; j++) {
        if (board[i][j] == 0) {                /* (i,j) ?????????????????????????????° */
            queen[i] = j;                      /* (i,j) ????????????????????????????????? */
            changeBoard(board, i, j, +1);      /* (i,j) ??????????¨???????????????????board??????+1?????? */
            setQueen(queen, board, i+1);       /* i+1 ??????????????????????????????????±??????? */
            changeBoard(board, i, j, -1);      /* (i,j) ??????????¨???????????????????board?????????????????? */
        }
    }
}

void findSolution()
{
    int queen[N];
    int board[N][N];
    int i;
    scanf("%d",&k);

    r = (int *)malloc(sizeof(int) * k);
    c = (int *)malloc(sizeof(int) * k);

    for(i = 0 ; i < k ; i++)
      scanf("%d %d",&r[i],&c[i]);
      
    memset(board, 0, sizeof(board));
    setQueen(queen, board, 0);
}

int main()
{
    findSolution();
    return 0;
}