Madoka and Underground Competitions (Codeforces 1717B)
Tags: Others, implementation

Madoka decided to participate in an underground sports programming competition. And there was exactly one task in it:A square table of size $$$n \times n$$$, where $$$n$$$ is a multiple of $$$k$$$, is called good if only the characters '.' and 'X' are written in it, as well as in any subtable of size $$$1 \times k$$$ or $$$k \times 1$$$, there is at least one character 'X'. In other words, among any $$$k$$$ consecutive vertical or horizontal cells, there must be at least one containing the character 'X'.Output any good table that has the minimum possible number of characters 'X', and also the symbol 'X' is written in the cell $$$(r, c)$$$. Rows are numbered from $$$1$$$ to $$$n$$$ from top to bottom, columns are numbered from $$$1$$$ to $$$n$$$ from left to right.

Input: The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Description of the test cases follows.The first and the only line of each test case contains four integers $$$n$$$, $$$k$$$, $$$r$$$, $$$c$$$ ($$$1 \le n \le 500, 1 \le k \le n, 1 \le r, c \le n$$$) — the size of the table, the integer $$$k$$$ and the coordinates of the cell, which must contain the character 'X'. It is guaranteed that $$$n$$$ is a multiple of $$$k$$$.It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$500$$$.

Output: For each test case, output $$$n$$$ lines, each consisting of $$$n$$$ characters '.' and 'X', — the desired table. If there are several answers, then you can output anyone.

Note: Let's analyze the first test case.The following tables can be printed as the correct answer: X....X.X. or ..XX...X.  It can be proved that there cannot be less than $$$3$$$ characters 'X' in the answer.Note that the following table is invalid because cell $$$(3, 2)$$$ does not contain the character 'X':  X...X...X In the second test case, the only correct table is:  XXXX  Each subtable of size $$$1 \times 1$$$ must contain a 'X' character, so all characters in the table must be equal to 'X'.

Sample Inputs:
3
3 3 3 2
2 1 1 2
6 3 4 2

Sample Outputs:
X..
..X
.X.
XX
XX
.X..X.
X..X..
..X..X
.X..X.
X..X..
..X..X

Solution (Python):
t = int(input())
for _ in range(t):
    a = list(map(int, input().split()))
    n = a[0]
    k = a[1]
    r = a[2] % k
    c = a[3] % k
    d = []
    e = ''
    for i in range(n):
        if i % k == 0:
            e += 'X'
        else:
            e += '.'
    for i in range(n):
        d.append(e[n - i % n:] + e[:n - i % n])
    if r - c < 0:
        w = abs(r - c)
    else:
        w = n - abs(r - c)
    for j in range(w, n):
        print(d[j])
    for x in range(w):
        print(d[x])

Solution (Python):
t = int(input())
for i in range(t):
    (n, k, r, c) = map(int, input().split())
    l = []
    for i in range(k):
        s = i * '.' + 'X' + (k - i - 1) * '.'
        l.append(s)
    if c % k == 0:
        a = k - 1
    else:
        a = c % k - 1
    if r % k == 0:
        b = k - 1
    else:
        b = r % k - 1
    x = l[b]
    l[b] = l[a]
    l[a] = x
    for i in range(k):
        l[i] = l[i] * int(n / k)
    l = l * int(n / k)
    for i in l:
        print(i)

Solution (Python):
for s in [*open(0)][1:]:
    (n, k, r, c) = map(int, s.split())
    s = n // k * ('X' + '.' * (k - 1)) * 4
    for j in range((i := (2 * n + r % k - c % k)), i - n, -1):
        print(s[j:j + n])