結果

問題 No.307 最近色塗る問題多くない?
ユーザー mkawa2
提出日時 2020-02-09 14:13:14
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,132 ms / 4,000 ms
コード長 1,152 bytes
コンパイル時間 420 ms
コンパイル使用メモリ 82,224 KB
実行使用メモリ 123,172 KB
最終ジャッジ日時 2024-10-01 05:54:41
合計ジャッジ時間 9,014 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 36
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

sys.setrecursionlimit(10 ** 6)

def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
dij = [(0, 1), (1, 0), (0, -1), (-1, 0)]

def main():
    def bfs(si, sj, x):
        pos=0
        stack[pos]=(si,sj)
        res=0
        while pos>=0:
            i,j=stack[pos]
            res+=1
            pos-=1
            for di, dj in dij:
                ni, nj = i + di, j + dj
                if ni < 0 or nj < 0 or ni >= h or nj >= w: continue
                if aa[ni][nj] == x: continue
                aa[ni][nj] = x
                pos+=1
                stack[pos]=(ni,nj)
        return res

    h, w = MI()
    aa = LLI(h)
    stack=[() for _ in range(h*w)]
    q=II()
    rcx=LLI(q)
    for r,c,x in rcx:
        r, c = r - 1, c - 1
        if aa[r][c] == x: continue
        aa[r][c] = x
        if bfs(r, c, x)==h*w:
            x=rcx[-1][-1]
            for i in range(h):
                aa[i]=[x]*w
            break
    for row in aa:print(*row)

main()
0