APCS Jun 2024 - o077 Q2 [Python]
Problem statement:
An H × W electronic canvas starts with every value set to 0, meaning uncolored. Simulate N brush operations. Each operation selects coordinates (r, c) and stays there for t seconds, adding color value x to all cells at Manhattan distance ≤ t. When colors overlap, their values are added. Output the canvas after all N operations.
H,W,N = map(int,input().split())
a = [[0 for _ in range(W)] for _ in range(H)]
for _ in range(N):
r,c,time,num = map(int,input().split())
for rr in range(r - time,r + time + 1):
for cc in range(c - time , c + time +1):
if 0 <= rr < H and 0 <= cc < W and abs(rr - r)+ abs(cc-c) <= time: # 曼哈頓距離公式
a[rr][cc] += num
for i in a:
for j in i:
print(j,end=' ')
print('\n', end='')
Important points
- Manhattan distance: On an orthogonal grid, this distance is the sum of the horizontal and vertical distances between two points. For
(r, c)and(rr, cc), the formula is:D = |rr - r| + |cc - c|
Key concepts:
- Simulation?
- [✔️] You may use the code directly if needed.
- [✔️] Please credit the source when sharing.

This article was translated from Traditional Chinese by AI. Meaning or wording may differ; please refer to the Traditional Chinese original as the authoritative version.