Feeding Chicken (Codeforces 1254A)
Tags: Greedy, constructive algorithms

Long is a huge fan of CFC (Codeforces Fried Chicken). But the price of CFC is increasing, so he decides to breed the chicken on his own farm.His farm is presented by a rectangle grid with $$$r$$$ rows and $$$c$$$ columns. Some of these cells contain rice, others are empty. $$$k$$$ chickens are living on his farm. The number of chickens is not greater than the number of cells with rice on the farm.Long wants to give his chicken playgrounds by assigning these farm cells to his chickens. He would like to satisfy the following requirements:  Each cell of the farm is assigned to exactly one chicken.  Each chicken is assigned at least one cell.  The set of cells assigned to every chicken forms a connected area. More precisely, if two cells $$$(x, y)$$$ and $$$(u, v)$$$ are assigned to the same chicken, this chicken is able to walk from $$$(x, y)$$$ to $$$(u, v)$$$ by passing only its cells and moving from each cell to another cell sharing a side. Long also wants to prevent his chickens from fighting for food. Hence he wants the difference between the maximum and the minimum number of cells with rice assigned to a chicken to be as small as possible. Please help him.

Input: Each test contains multiple test cases. The first line contains the number of test cases $$$T$$$ ($$$1 \le T \le 2 \cdot 10^4$$$). Description of the test cases follows.The first line of each test case contains three integers $$$r$$$, $$$c$$$ and $$$k$$$ ($$$1 \leq r, c \leq 100, 1 \leq k \leq 62$$$), representing the size of Long's farm and the number of chickens Long has. Each of the next $$$r$$$ lines contains $$$c$$$ characters, each is either "." or "R", representing an empty cell or a cell with rice. It is guaranteed that the number of chickens is not greater than the number of cells with rice on the farm.It is guaranteed that the sum of $$$r \cdot c$$$ over all test cases does not exceed $$$2 \cdot 10^4$$$.

Output: For each test case, print $$$r$$$ lines with $$$c$$$ characters on each line. Each character should be either a lowercase English character, an uppercase English character, or a digit. Two characters should be equal if and only if the two corresponding cells are assigned to the same chicken. Uppercase and lowercase characters are considered different, so "A" and "a" belong to two different chickens.If there are multiple optimal answers, print any.

Note: These pictures explain the sample output. Each color represents one chicken. Cells filled with patterns (not solid colors) contain rice.In the first test case, each chicken has one cell with rice. Hence, the difference between the maximum and the minimum number of cells with rice assigned to a chicken is $$$0$$$.In the second test case, there are $$$4$$$ chickens with $$$3$$$ cells of rice, and $$$2$$$ chickens with $$$2$$$ cells of rice. Hence, the difference between the maximum and the minimum number of cells with rice assigned to a chicken is $$$3 - 2 = 1$$$.In the third test case, each chicken has $$$3$$$ cells with rice. In the last test case, since there are $$$62$$$ chicken with exactly $$$62$$$ cells of rice, each chicken must be assigned to exactly one cell. The sample output is one of the possible way.

Sample Inputs:
4
3 5 3
..R..
...R.
....R
6 4 6
R..R
R..R
RRRR
RRRR
R..R
R..R
5 5 4
RRR..
R.R..
RRR..
R..R.
R...R
2 31 62
RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR
RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR

Sample Outputs:
11122
22223
33333
aacc
aBBc
aBBc
CbbA
CbbA
CCAA
11114
22244
32444
33344
33334
abcdefghijklmnopqrstuvwxyzABCDE
FGHIJKLMNOPQRSTUVWXYZ0123456789

Solution (Python):
t = int(input())
for _ in range(t):
    (r, c, k) = map(int, input().split())
    farm = []
    rice = 0
    chk = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
    for i in range(r):
        f = input()
        rice += f.count('R')
        farm.append(f)
    n = rice // k
    rest = rice % k
    start = 0
    cnt = [0] * k
    for i in range(r):
        out = ''
        for j in range(c):
            if farm[i][j if i % 2 == 0 else c - j - 1] == 'R':
                if cnt[start] < n:
                    cnt[start] += 1
                    out += chk[start]
                elif rest > 0:
                    cnt[start] += 1
                    rest -= 1
                    out += chk[start]
                    start += 1
                else:
                    cnt[start + 1] += 1
                    out += chk[start + 1]
                    start += 1
            else:
                out += chk[start]
        print(out if i % 2 == 0 else out[::-1])

Solution (Python):
C = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
T = int(input())
for t in range(T):
    (r, c, k) = map(int, input().split())
    A = []
    for i in range(r):
        A.append(list(input()))
    tot = 0
    for i in range(r):
        tot += A[i].count('R')
    target = [tot // k + 1 if i < tot % k else tot // k for i in range(k)]
    for i in range(r):
        if i % 2:
            A[i].reverse()
    cid = 0
    cur = 0
    for i in range(r):
        for j in range(c):
            if A[i][j] == 'R':
                cur += 1
            A[i][j] = C[cid]
            if cur == target[cid] and cid != k - 1:
                cid += 1
                cur = 0
    for i in range(r):
        if i % 2:
            A[i].reverse()
        print(''.join(A[i]))

Solution (Python):
from string import ascii_letters, digits

def snake(n, m):
    i = j = 0
    d = 1
    while i < n:
        while 0 <= j < m:
            yield (i, j)
            j += d
        i += 1
        d *= -1
        j += d
chickens = ascii_letters + digits
cfield = [[''] * 100 for _ in range(100)]
rfield = [''] * 100
for _ in range(int(input())):
    (r, c, k) = map(int, input().split())
    n = 0
    for i in range(r):
        rfield[i] = input()
        n += rfield[i].count('R')
    q = n // k
    a = n % k
    u = 0
    v = q + int(u < a)
    for (i, j) in snake(r, c):
        cfield[i][j] = chickens[min(u, k - 1)]
        if rfield[i][j] == 'R':
            v -= 1
            if v == 0:
                u += 1
                v = q + int(u < a)
    for i in range(r):
        print(*cfield[i][:c], sep='')