結果

問題 No.1668 Grayscale
ユーザー LyricalMaestro
提出日時 2025-03-01 20:13:09
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,947 bytes
コンパイル時間 279 ms
コンパイル使用メモリ 82,408 KB
実行使用メモリ 235,572 KB
最終ジャッジ日時 2025-03-01 20:13:19
合計ジャッジ時間 8,952 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 16 WA * 6
権限があれば一括ダウンロードができます

ソースコード

diff #

## https://yukicoder.me/problems/no/1668

from collections import deque

def main():
    H, W, N = map(int, input().split())
    C = []
    for _ in range(H):
        C.append(list(map(int, input().split())))

    queues = [[] for _ in range(N)]    
    passed = [[-1] * W for _ in range(H)]
    colors = [[-1] * W for _ in range(H)]

    for h in range(H):
        for w in range(W):
            c = C[h][w]
            queues[c - 1].append((h, w))
    
    max_xx = 0
    for n in range(N):
        array = queues[n]

        for s_h, s_w in array:
            if  passed[s_h][s_w] == -1:
                max_x = max_xx
                queue = deque()
                queue.append((s_h, s_w))

                ans_array = [(s_h, s_w)]
                passed[s_h][s_w] = 1

                while len(queue) > 0:
                    h, w = queue.popleft()
                    for dh, dw in ((0, 1), (0, -1), (1, 0), (-1, 0)):
                        new_h = dh + h
                        new_w = dw + w
                        if 0 <= new_h < H and 0 <= new_w < W:
                            if C[new_h][new_w] > C[h][w]:
                                continue
                            elif C[new_h][new_w] < C[h][w]:
                                max_x = max(max_x, colors[new_h][new_w])
                            else:
                                if passed[new_h][new_w] == -1:
                                    passed[new_h][new_w] = 1
                                    ans_array.append((new_h, new_w))
                                    queue.append((new_h, new_w))
                
                for h, w in ans_array:
                    colors[h][w] = max_x + 1
                max_xx = max_x

    answer = -1
    for h in range(H):
        for w in range(W):
            answer = max(answer, colors[h][w])
    print(answer)
                        






    


        

    



if __name__ == "__main__":
    main()
0