This page is still under construction.

Parts of this page are still being built. What you see may change.

Rooks

Interview

Time limit1sMemory limit128 MB

Summary
Complete a partially filled n by n board to a full non-attacking rook placement, choosing the lexicographically smallest completion.
Level

Medium6 of 10

Topics
Greedy, Sorting, Implementation, Array
Solved
No attempts yet

Problem

After a lot of effort, Bytie has finally managed to place nn rooks on an n×nn \times n board so that no two of them attack each other. As a reminder, a rook attacks every square that lies in the same row or the same column as it.

Unfortunately, Bytie has now accidentally bumped the board and several rooks have fallen off. Leaving every rook that is still on the board exactly where it is, help Bytie put the fallen rooks back so that all nn rooks are on the board again and no two of them attack each other.

Input

The first line contains one integer nn (2≤n≤1 0002 \le n \le 1\,000), the size of the board. Each of the next nn lines contains nn characters describing the current configuration. The character '.' is an empty square, and the character 'W' is a square occupied by a rook.

There are ww rooks on the board with 1≤w≤n−11 \le w \le n - 1, and no two of them attack each other.

Output

Print nn lines of nn characters each ('.' or 'W') describing the completed board. It must contain exactly nn rooks, keep every initial rook in its original position, and have no two rooks attacking each other.

Because the n−wn - w fallen rooks can be placed in more than one way, print the lexicographically smallest such board. Treat the board as the string formed by its output lines read top to bottom, each line left to right, and consider '.' to come before 'W'.

Examples4

  1. Example 1

    Input
    8
    ........
    .....W..
    ..W.....
    .......W
    W.......
    ........
    .W......
    ........
    
    Expected output
    ......W.
    .....W..
    ..W.....
    .......W
    W.......
    ....W...
    .W......
    ...W....
    
  2. Example 2

    Input
    2
    W.
    ..
    
    Expected output
    W.
    .W
    
  3. Example 3

    Input
    2
    ..
    .W
    
    Expected output
    W.
    .W
    
  4. Example 4

    Input
    3
    ...
    .W.
    ...
    
    Expected output
    ..W
    .W.
    W..