Feeding Chicken (Codeforces 1255D)
Tags: Greedy, implementation

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):
from sys import stdin, stdout
''
T = int(stdin.readline().strip())

def fill(x, y, k, dr, let):
    while True:
        if dr == 1:
            for i in range(y, m):
                if mt[x][i] == 'R':
                    if k == 0:
                        return [x, i, dr]
                    k -= 1
                ans[x][i] = let
        else:
            for i in range(y, -1, -1):
                if mt[x][i] == 'R':
                    if k == 0:
                        return [x, i, dr]
                    k -= 1
                ans[x][i] = let
        dr = (dr + 1) % 2
        if dr == 1:
            y = 0
        else:
            y = m - 1
        x += 1
        if x == n:
            return [x, x, x]
lt = [chr(ord('0') + i) for i in range(10)] + [chr(ord('a') + i) for i in range(26)] + [chr(ord('A') + i) for i in range(26)]
for caso in range(T):
    (n, m, g) = map(int, stdin.readline().strip().split())
    ans = [['' for j in range(m)] for i in range(n)]
    r = 0
    mt = []
    for i in range(n):
        mt.append(stdin.readline().strip())
        r += mt[-1].count('R')
    x = r // g
    y = -1
    z = 0
    for i in range(g, -1, -1):
        y = r - x * i
        if y >= 0 and y % (x + 1) == 0 and (y // (x + 1) == g - i):
            z = y // (x + 1)
            y = i
            break
    cx = 0
    cy = 0
    dr = 1
    ind = 0
    aux = [0, 0, 1]
    for i in range(g):
        if i < y:
            aux = fill(aux[0], aux[1], x, aux[2], lt[i])
        else:
            aux = fill(aux[0], aux[1], x + 1, aux[2], lt[i])
    for i in ans:
        aux = ''
        for j in i:
            aux += j
        stdout.write('%s\n' % aux)

Solution (Python):
for _ in range(int(input())):
    (r, c, k) = [int(i) for i in input().split()]
    d = [list(input()) for i in range(r)]
    rice = sum((dd.count('R') for dd in d))
    cnt = [rice // k] * k
    for i in range(rice % k):
        cnt[i] += 1
    ans = [[-1] * c for i in range(r)]
    cur_ch = 0
    for i in range(r):
        for sn_c in range(c):
            j = sn_c if i & 1 else c - 1 - sn_c
            ans[i][j] = cur_ch
            if d[i][j] == 'R':
                cnt[cur_ch] -= 1
                if cnt[cur_ch] == 0 and cur_ch < k - 1:
                    cur_ch += 1
    abc = '1234567890QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm'
    for rw in ans:
        print(''.join((abc[i] for i in rw)))

Solution (Python):
for _ in range(int(input())):
    (row, col, k) = map(int, input().split())
    arr = []
    for i in range(row):
        s = input()
        if i % 2 == 0:
            temp = [x for x in s]
        else:
            temp = [x for x in s[::-1]]
        arr.extend(temp)
    ric = arr.count('R')
    ans = []
    c = 65
    j = 0
    for i in range(1, k + 1):
        rc = 0
        if c == 65 + 26:
            c = 97
        elif c == 97 + 26:
            c = 48
        while True and j < row * col:
            if arr[j] == 'R':
                mc = ric // k
                if i <= ric % k:
                    mc += 1
                if rc >= mc:
                    c += 1
                    break
                rc += 1
            ans.append(chr(c))
            j += 1
    for i in range(row):
        temp = ans[i * col:(i + 1) * col]
        if i % 2 != 0:
            temp = temp[::-1]
        print(*temp, sep='')