codeforces-scrape

Codeforces scraped problems and editorials
git clone git://git.laack.co/codeforces-scrape.git
Log | Files | Refs | README

1863D.md (2678B)


      1 Source: https://codeforces.com/problemset/problem/1863/D
      2 
      3 D. Two-Colored Dominoes
      4 
      5 time limit per test
      6 
      7 1 second
      8 
      9 memory limit per test
     10 
     11 256 megabytes
     12 
     13 input
     14 
     15 standard input
     16 
     17 output
     18 
     19 standard output
     20 
     21 There is an $n\times m$ board divided into cells. There are also some dominoes on this board. Each domino covers two adjacent cells (that is, two cells that share a side), and no two dominoes overlap.
     22 
     23 Piet thinks that this board is too boring and it needs to be painted. He will paint the cells of the dominoes black and white. He calls the painting beautiful if all of the following conditions hold:
     24 
     25 * for each domino, one of its cells is painted white and the other is painted black;
     26 * for each row, the number of black cells in this row equals the number of white cells in this row;
     27 * for each column, the number of black cells in this column equals the number of white cells in this column.
     28 
     29 Note that the cells that are not covered by dominoes are not painted at all, they are counted as neither black nor white.
     30 
     31 Help Piet produce a beautiful painting or tell that it is impossible.
     32 
     33 Input
     34 
     35 Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10\,000$). The description of the test cases follows.
     36 
     37 The first line of each test case contains two integers $n$ and $m$ ($2\le n, m\le 500$).
     38 
     39 The next $n$ lines describe the tiling of the board, row by row from top to bottom. Each of these lines contains $m$ characters, describing the cells in the corresponding row from left to right. Each character is one of U, D, L, R, or ., meaning that the cell is covered with a top, bottom, left, right half of a domino or nothing, respectively. The tiling is guaranteed to be valid.
     40 
     41 It is guaranteed that the sum of $n \cdot m$ over all test cases does not exceed $250\,000$.
     42 
     43 Output
     44 
     45 For each test case, output a single integer $-1$ if a beautiful painting does not exist. Otherwise, print $n$ lines, each containing $m$ characters, describing the colors in the corresponding row of a beautiful painting. Every character corresponding to a cell not covered by a domino must be . (a dot), and all other characters must be B if the corresponding cell is black or W if it is white.
     46 
     47 If there are multiple solutions, print any of them.
     48 
     49 Example
     50 
     51 Input
     52 
     53 ```
     54 3
     55 
     56 4 6
     57 
     58 ..LR..
     59 
     60 ULRU..
     61 
     62 DLRDUU
     63 
     64 ..LRDD
     65 
     66 5 4
     67 
     68 .LR.
     69 
     70 .UU.
     71 
     72 UDDU
     73 
     74 D..D
     75 
     76 LR..
     77 
     78 2 2
     79 
     80 ..
     81 
     82 ..
     83 ```
     84 
     85 Output
     86 
     87 ```
     88 ..WB..
     89 WWBB..
     90 BBWWWB
     91 ..BWBW
     92 -1
     93 ..
     94 ..
     95 ```
     96 
     97 Note
     98 
     99 In the first test case, the answer is illustrated below:
    100 
    101  ![](https://espresso.codeforces.com/0d4ae604da1f0ff16feac7ecbc573026ca45e004.png) 
    102 
    103 In the second test case, it is impossible to paint all cells the right way.