B - モザイクアートの作成 / Creating Mosaic Art 解説 by admin
GPT 5.2 HighOverview
Given an \(H \times W\) grid consisting of # and . as the original design, we scale it by \(K\) times both vertically and horizontally, replacing # with \(c_1\) and . with \(c_2\), and output the resulting \(HK \times WK\) string.
Approach
The key point is “turning each cell into a \(K \times K\) block of the same character.” While this can be solved by preparing a 2D array of size \(HK \times WK\) and filling it cell by cell, there’s no need to create such a large array.
By observation, each row of the scaled output can be constructed as follows:
- Look at each character from left to right in one row (length \(W\)) of the original design:
- If
#, repeat character \(c_1\) \(K\) times - If
., repeat character \(c_2\) \(K\) times
- If
- The resulting “scaled row (length \(WK\))” is then repeated \(K\) times vertically
For example, when \(K=3\), a single character # in the original becomes c1c1c1 horizontally, and by outputting the same row 3 times, it forms a \(3 \times 3\) block.
Naively concatenating strings one character at a time with += can be slow depending on the language and implementation, but in Python the standard approach is to collect fragments in a list and join them at the end with ''.join(...) (which is what the code here does).
Algorithm
- Read the input: \(H, W, K, c_1, c_2\) and each row \(S_i\) of the original design.
- For each row \(S_i\), do the following:
- For each character
ch, createc1*Kif it is#orc2*Kif it is., and concatenate them in order to form the scaled rowline(length \(WK\)). - Add that
lineto the output \(K\) times for the vertical scaling.
- For each character
- Finally, output all rows separated by newlines.
Complexity
- Time complexity: \(O(H \cdot W \cdot K)\) (The total number of characters in the output is \(HK \cdot WK = H W K^2\), but in the implementation we create a string of length \(K\) for each cell, so the amount of character generation is proportional to the final output)
- Space complexity: \(O(H \cdot W \cdot K^2)\) (since we store the entire output in
out_lines) *If we print incrementally, we can avoid storing everything and reduce this to about \(O(WK)\).
Implementation Notes
Horizontal scaling is easily done by
tile * Kall at once (e.g.,'a'*3 -> 'aaa').For string concatenation, it is efficient to collect fragments in a list and join them at the end with
''.join(...).Vertical scaling is achieved simply by repeating the constructed row \(K\) times and appending it to the output (
out_lines.extend([line] * K)).Source Code
import sys
def main():
it = iter(sys.stdin.read().split())
H = int(next(it))
W = int(next(it))
K = int(next(it))
c1 = next(it)
c2 = next(it)
out_lines = []
for _ in range(H):
s = next(it)
expanded = []
for ch in s:
tile = c1 if ch == '#' else c2
expanded.append(tile * K)
line = ''.join(expanded)
out_lines.extend([line] * K)
sys.stdout.write("\n".join(out_lines))
if __name__ == "__main__":
main()
This editorial was generated by gpt-5.2-high.
投稿日時:
最終更新: