結果

問題 No.307 最近色塗る問題多くない?
ユーザー H3PO4
提出日時 2022-08-21 09:22:24
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 828 ms / 4,000 ms
コード長 1,012 bytes
コンパイル時間 439 ms
コンパイル使用メモリ 82,560 KB
実行使用メモリ 82,944 KB
最終ジャッジ日時 2024-10-10 02:16:33
合計ジャッジ時間 7,570 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 36
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from collections import deque

input = sys.stdin.buffer.readline

H, W = map(int, input().split())
table = list(list(map(int, input().split())) for _ in range(H))
flag_all = False
final_color = 0
Q = int(input())
for _ in range(Q):
    R, C, X = map(int, input().split())
    R -= 1
    C -= 1
    if flag_all:
        final_color = X
    else:
        if table[R][C] == X:
            continue
        table[R][C] = X
        d = deque([(R, C)])
        ct = 0
        while d:
            r, c = d.pop()
            ct += 1
            for dr, dc in ((1, 0), (0, 1), (-1, 0), (0, -1)):
                rdr = r + dr
                cdc = c + dc
                if 0 <= rdr < H and 0 <= cdc < W and table[rdr][cdc] != X:
                    table[rdr][cdc] = X
                    d.append((rdr, cdc))
        if ct == H * W:
            flag_all = True
            final_color = X
if flag_all:
    for _ in range(H):
        print(*([final_color] * W))
else:
    for row in table:
        print(*row)
0