結果

問題 No.2064 Smallest Sequence on Grid
ユーザー H20
提出日時 2022-09-02 22:30:58
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,468 ms / 3,000 ms
コード長 768 bytes
コンパイル時間 198 ms
コンパイル使用メモリ 82,052 KB
実行使用メモリ 439,680 KB
最終ジャッジ日時 2024-11-16 04:34:22
合計ジャッジ時間 22,931 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 29
権限があれば一括ダウンロードができます

ソースコード

diff #

H,W = map(int, input().split())
S = []
for _ in range(H):
    S.append(list(input()+'~'))
S.append(list('~'*(W+2)))

pos = set()
pos.add(0)
ANS = [S[0][0]]

while len(ANS)<H+W-1:
    alphabet = 'z'
    nextPos = set()
    for xy in list(pos):
        x,y = xy%W,xy//W
        if S[y][x+1]==alphabet:
            nextPos.add(xy+1)
        elif S[y][x+1]<alphabet:
            alphabet = S[y][x+1]
            nextPos = set()
            nextPos.add(xy+1)
        if not xy+W in nextPos:
            if S[y+1][x]==alphabet:
                nextPos.add(xy+W)
            elif S[y+1][x]<alphabet:
                alphabet = S[y+1][x]
                nextPos = set()
                nextPos.add(xy+W)
    ANS.append(alphabet)
    pos = nextPos
print(''.join(map(str, ANS)))
0