問題一覧 > 通常問題

No.307 Aren't There a Lot of Coloring Problems Lately?

レベル : / AI推定難易度 : 1300 / 実行時間制限 : 1ケース 4.000秒 / メモリ制限 : 256 MB / 標準ジャッジ問題
タグ : (AC するまで非表示) / 解いたユーザー数 88
作問者 : koyumeishi
お気に入りにしたユーザー ProblemId : 769 / 自分の提出
問題文最終更新日: 2026-06-25 01:57:14
This statement was translated by AI (claude-opus-5-5, effort medium) and may contain errors or differ in nuance from the original. The original statement takes precedence. Back to the original
yukicoder contest 128 (順位表) の他の問題:

Problem Statement

Consider filling the cells of a sheet of grid paper with $H$ cells vertically and $W$ cells horizontally using 2 colors of ink.

The cell in the $R$-th row from the top and the $C$-th column from the left is denoted by $(R,C)$. The top-left cell is $(1,1)$ and the bottom-right cell is $(H,W)$.
Cell $(R,C)$ is initially painted in color $A_{R,C}$.

For a cell $(R,C)$ and a cell $(R^\prime,C^\prime)$ adjacent to it, when the 2 cells have the same color, we say that "$(R,C)$ and $(R^\prime,C^\prime)$ are connected".
Also, when $(R,C)$ and $(R^\prime,C^\prime)$ are connected and $(R^\prime,C^\prime)$ and $(R^{\prime\prime},C^{\prime\prime})$ are connected, we say that $(R,C)$ and $(R^{\prime\prime},C^{\prime\prime})$ are connected as well.
Here, "adjacent" refers to the case where the Manhattan distance between $(R,C)$ and $(R^\prime,C^\prime)$ is $1$, that is, where $(R^\prime,C^\prime)$ is one of $(R+1,C), (R-1,C), (R,C+1), (R,C-1)$.

You are given $Q$ queries of the form "fill all cells connected to cell $(R,C)$ with color $X$".
Output the color of each cell of the grid paper after all $Q$ fills have been performed.

In short, this is the "fill" tool of a paint program.

Input

$H$ $W$
$A_{1,1}$ $A_{1,2}$ $\dots$ $A_{1,W}$
$\vdots$
$A_{H,1}$ $A_{H,2}$ $\dots$ $A_{H,W}$
$Q$
$R_1$ $C_1$ $X_1$
$\vdots$
$R_Q$ $C_Q$ $X_Q$

All input values are integers and satisfy the following constraints.
$1 \leq H,W \leq 200$
$1 \leq Q \leq 5 \times 10^4$
$0 \leq A_{R,C}, X_i \leq 1$
$1 \leq R_i \leq H$
$1 \leq C_i \leq W$

Output

$A^\prime_{1,1}$ $A^\prime_{1,2}$ $\dots$ $A^\prime_{1,W}$
$\vdots$
$A^\prime_{H,1}$ $A^\prime_{H,2}$ $\dots$ $A^\prime_{H,W}$

Output the color $A^\prime_{R,C}$ of each cell after the fills.

Sample

Sample 1
Input
3 3
0 1 0
1 0 1
0 1 0
1
2 2 1
Output
0 1 0
1 1 1
0 1 0

Sample 2
Input
4 4
0 0 1 0
1 1 0 1
0 1 0 0
1 1 1 1
2
3 3 1
2 1 0
Output
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0

This part

0 0 1 0
1 1 * 1
0 1 * *
1 1 1 1
becomes this, and
0 0 1 0
1 1 1 1
0 1 1 1
1 1 1 1
this part
0 0 * 0
* * * *
0 * * *
* * * *
becomes this.
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0

Sample 3
Input
5 5
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
3
1 1 1
2 2 1
3 3 1
Output
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1

Sample 4
Input
5 3
1 0 1
0 0 1
1 1 0
1 0 1
1 0 1
3
1 1 0
4 3 1
5 3 0
Output
0 0 1
0 0 1
1 1 0
1 0 0
1 0 0

提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。