結果

問題 No.2064 Smallest Sequence on Grid
ユーザー ntuda
提出日時 2022-09-30 14:38:54
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 816 bytes
コンパイル時間 490 ms
コンパイル使用メモリ 82,044 KB
実行使用メモリ 198,248 KB
最終ジャッジ日時 2024-12-22 18:51:27
合計ジャッジ時間 40,729 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 20 TLE * 9
権限があれば一括ダウンロードができます

ソースコード

diff #

from heapq import heappush, heappop

H, W = map(int, input().split())
S = [input() for _ in range(H)]
T = [S[0][0]]
Q = [(0, 0)]
for _ in range(H + W - 2):
    # 次の候補の座標をQ2に追加
    # 重複するけど気にしない
    Q2 = []
    h0 = -1
    for h, w in Q:
        if w < W - 1 and h > h0:
            heappush(Q2,(S[h][w + 1], h, w + 1))
            h0 = h
        if h < H - 1 and h + 1 > h0:
            heappush(Q2,(S[h + 1][w], h + 1, w))
            h0 = h + 1
    # Qを空にしてから文字が最小の物だけ抽出してQに追加
    Q = []
    c,h,w = heappop(Q2)
    c0 = c
    T.append(c)
    #print("Q2",Q2)
    while c == c0:
        Q.append((h, w))
        if len(Q2) == 0:
            break
        c,h,w = heappop(Q2)
    Q.sort()
    #print("Q",Q)

print("".join(T))
0