Official

B - モザイクアートの作成 / Creating Mosaic Art Editorial by admin

Claude 4.6 Opus (Thinking)

Overview

This problem asks you to enlarge each cell of the design draft into a \(K \times K\) block and output a mosaic art by replacing # and . with their respective specified characters.

Approach

There are two main operations to perform in this problem:

  1. Enlargement: Convert each cell of the draft into a \(K \times K\) block of the same character.
  2. Color assignment: Replace # with character \(c_1\) and . with character \(c_2\).

Let’s consider a concrete example. Suppose the draft is \(2 \times 3\) with \(K = 2\):

#.#
..#

Horizontal enlargement (duplicate each character \(K=2\) times): - #.###..## - ..#....##

Vertical enlargement (repeat each row \(K=2\) times):

##..##
##..##
....##
....##

Color assignment (e.g., \(c_1 = \) A, \(c_2 = \) B):

AABBAA
AABBAA
BBBBAA
BBBBAA

An important observation is that horizontal enlargement and color assignment can be processed simultaneously. For each character, if it is #, place \(K\) copies of \(c_1\); if it is ., place \(K\) copies of \(c_2\). This completes one row of the output string. Vertical enlargement is achieved simply by outputting that row \(K\) times.

Even with a straightforward implementation, the constraints are small enough (the maximum output size is about \(100 \times 50 \times 100 \times 50 = 25{,}000{,}000\) characters), so there is no need to worry about TLE.

Algorithm

  1. Read \(H\), \(W\), \(K\), \(c_1\), \(c_2\).
  2. For each row \(S\) of the draft, do the following:
    • For each character ch in \(S\), if ch is #, repeat \(c_1\) \(K\) times; if ., repeat \(c_2\) \(K\) times. Concatenate these to form the enlarged row row.
    • Output row \(K\) times (vertical enlargement).

Complexity

  • Time complexity: \(O(HK \times WK)\)
    • For each row (out of \(H\) rows), we construct a string of length \(WK\) and output it \(K\) times, so the total work is proportional to the output size.
  • Space complexity: \(O(WK)\)
    • We only need to store one row’s worth of the string.

Implementation Notes

  • Perform horizontal enlargement and color assignment at once: For each character, generate c1 * K or c2 * K, then concatenate with ''.join(...). This completes both horizontal enlargement and character replacement simultaneously.

  • Vertical enlargement is just outputting the same row \(K\) times: Save the enlarged row in a variable row, then simply print(row) \(K\) times in an inner loop.

  • Processing row by row is memory-efficient: There is no need to store the entire grid in a two-dimensional array; you can complete the output as you read each row.

    Source Code

H, W, K = map(int, input().split())
c1, c2 = input().split()
for _ in range(H):
    S = input()
    row = ''.join(c1 * K if ch == '#' else c2 * K for ch in S)
    for _ in range(K):
        print(row)

This editorial was generated by claude4.6opus-thinking.

posted:
last update: