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 Ruby (judge verdict: Accepted)

# バックトラック法
#   http://sevendays-study.com/algorithm/ex-day4.html

class Queen

  N = 8
  EMPTY = '-'
  FULL = '.'
  QUEEN = 'Q'

  def initialize
    @board = Array.new(N){ Array.new(N){ EMPTY } } # 盤面情報
    @rows = Array.new(N){ EMPTY } # 各行が埋まっているかどうか
    @cols = Array.new(N){ EMPTY } # 各列が埋まっているかどうか
    @history = [] # バックトラック用の履歴
  end

  def put(x, y)
    # return 置ける場合はtrue, 置けない場合はfalse

    return false if @board[x][y] != EMPTY

    save(x, y)

    #
    @rows[y] = FULL
    @cols[x] = FULL

    # 行列方向
    0.upto(N-1) do | a |
      @board[x][a] = FULL
      @board[a][y] = FULL
    end

    # Queen
    @board[x][y] = QUEEN

    # 斜め方向
    [[1,1], [1,-1], [-1,1], [-1,-1]].each do | direction_x, direction_y |
      _x, _y = x + direction_x, y + direction_y
      while 0<=_x && _x<=7 && 0<=_y && _y<=7
        @board[_x][_y] = FULL
        _x, _y = _x + direction_x, _y + direction_y
      end
    end
    return true
  end

  def solve(x_start, y_start)
    x_start.upto(N-1) do | x |
      if @cols[x] == EMPTY
        could = false # 該当行に置けたか
        y_start.upto(N-1) do | y |
          if put(x, y)
            could = true
            break
          end
        end
        y_start = 0
        unless could
          x, y = rollback
          solve(x, y+1)
        end
      end
    end
  end

  def print
    @board.each{|r| puts r.join('').gsub('-', '.')}
  end

  def debug_print
    puts '----'
    p @history
    @board.each{|r| puts r.join('').gsub('-', '.')}
  end

  private
  def save(x, y)
    @history << [deep_copy, @rows.clone, @cols.clone, x, y]
  end
  def rollback
    @board, @rows, @cols, x, y = @history.pop
    return x, y
  end
  def deep_copy
    new_board = Array.new(N){ Array.new(N){ EMPTY } }
    0.upto(N-1) do | x |
      0.upto(N-1) do | y |
        new_board[x][y] = @board[x][y]
      end
    end
    return new_board
  end
end

q = Queen.new

n = gets.to_i
n.times do
  x, y = gets.split.map(&:to_i)
  q.put(x, y)
end
q.solve(0, 0)
q.print