Colour the Flag (Codeforces 1534A)
Tags: Implement, brute force

Today we will be playing a red and white colouring game (no, this is not the Russian Civil War; these are just the colours of the Canadian flag).You are given an $$$n \times m$$$ grid of "R", "W", and "." characters. "R" is red, "W" is white and "." is blank. The neighbours of a cell are those that share an edge with it (those that only share a corner do not count).Your job is to colour the blank cells red or white so that every red cell only has white neighbours (and no red ones) and every white cell only has red neighbours (and no white ones). You are not allowed to recolour already coloured cells.

Input: The first line contains $$$t$$$ ($$$1 \le t \le 100$$$), the number of test cases.In each test case, the first line will contain $$$n$$$ ($$$1 \le n \le 50$$$) and $$$m$$$ ($$$1 \le m \le 50$$$), the height and width of the grid respectively.The next $$$n$$$ lines will contain the grid. Each character of the grid is either 'R', 'W', or '.'.

Output: For each test case, output "YES" if there is a valid grid or "NO" if there is not.If there is, output the grid on the next $$$n$$$ lines. If there are multiple answers, print any.In the output, the "YES"s and "NO"s are case-insensitive, meaning that outputs such as "yEs" and "nO" are valid. However, the grid is case-sensitive.

Note: The answer for the first example case is given in the example output, and it can be proven that no grid exists that satisfies the requirements of the second example case. In the third example all cells are initially coloured, and the colouring is valid.

Sample Inputs:
3
4 6
.R....
......
......
.W....
4 4
.R.W
....
....
....
5 1
R
W
R
W
R

Sample Outputs:
YES
WRWRWR
RWRWRW
WRWRWR
RWRWRW
NO
YES
R
W
R
W
R

Solution (Python):
I = input
r = range
for _ in [0] * int(I()):
    (n, m) = map(int, I().split())
    f = (s := ({1 & i + j + ord(x) for i in r(n) for (j, x) in zip(r(m), I()) if '.' < x} or [0])).pop()
    print(*(['YES'] + [('RWWR'[f + i & 1::2] * m)[:m] for i in r(n)], ['NO'])[len(s)])

Solution (Python):
import sys
input = sys.stdin.readline

def readList():
    return list(map(int, input().split()))

def readInt():
    return int(input())

def readInts():
    return map(int, input().split())

def readStr():
    return input().strip()

def solve():
    (n, m) = readInts()
    res0 = [list(readStr()) for _ in range(n)]
    res1 = [list(arr) for arr in res0]
    d0 = d1 = 1
    for i in range(n):
        for j in range(m):
            if i % 2 == 0:
                c0 = 'W' if j % 2 else 'R'
                c1 = 'R' if c0 == 'W' else 'W'
            else:
                c0 = 'R' if j % 2 else 'W'
                c1 = 'R' if c0 == 'W' else 'W'
            if res0[i][j] == '.':
                res0[i][j] = c0
                res1[i][j] = c1
            else:
                if res0[i][j] != c0:
                    d0 = 0
                if res1[i][j] != c1:
                    d1 = 0
        res0[i] = ''.join(res0[i])
        res1[i] = ''.join(res1[i])
    if d0:
        return '\n'.join(['YES'] + res0)
    elif d1:
        return '\n'.join(['YES'] + res1)
    else:
        return 'NO'
for _ in range(int(input())):
    print(solve())

Solution (Python):
I = input
r = range
for _ in [0] * int(I()):
    (n, m) = map(int, I().split())
    f = (s := ({1 & i + j + ord(x) for i in r(n) for (j, x) in zip(r(m), I()) if '.' < x} or [0])).pop()
    print(*(['YES'] + [('RWWR'[f + i & 1::2] * m)[:m] for i in r(n)], ['NO'])[len(s)])