公式

A - 穴あきカード / Punched Card 解説 by admin

Qwen3-Coder-480B

Overview

Given a card represented by an \(N \times M\) grid, punch holes (.) at \(K\) specified locations.

Analysis

This problem is a straightforward simulation problem. The initial state of the card is entirely # (no holes), and we simply need to replace the specified positions with ..

The important point is that the coordinates given in the input are 1-indexed (1-origin), whereas most programming languages (especially arrays) use 0-indexed (0-origin). Therefore, we need to convert \((R_i, C_i)\) to \((R_i - 1, C_i - 1)\) when accessing the array.

For example, when punching a hole at \((2, 3)\) on a \(3 \times 3\) card, we internally set card[1][2] to ..

In this problem, it is guaranteed that no instruction will punch a hole at the same location twice, so there is no need to worry about duplicate handling.

Algorithm

  1. Initialize an \(N \times M\) two-dimensional list with #.
  2. For each of the \(K\) instructions, replace \((R_i, C_i)\) with . (converting to 0-indexed).
  3. Convert each row to a string and output it.

Complexity

  • Time complexity: \(O(N \times M + K)\)
    • \(O(N \times M)\) for grid initialization, \(O(K)\) for writing the \(K\) positions.
  • Space complexity: \(O(N \times M)\)
    • For storing the entire grid.

Implementation Notes

  • Don’t forget the coordinate conversion (1-indexed → 0-indexed).
  • Output each row of the two-dimensional list as a string using ''.join() or similar.
## Source Code

```python
N, M, K = map(int, input().split())

# カードの初期状態を作成(すべて '#')
card = [['#' for _ in range(M)] for _ in range(N)]

# 穴を開ける位置を処理
for _ in range(K):
    R, C = map(int, input().split())
    card[R-1][C-1] = '.'  # 0-indexed に変換

# 結果を出力
for row in card:
    print(''.join(row))

This editorial was generated by qwen3-coder-480b.

投稿日時:
最終更新: