結果
| 問題 | 
                            No.2064 Smallest Sequence on Grid
                             | 
                    
| コンテスト | |
| ユーザー | 
                             | 
                    
| 提出日時 | 2022-09-24 17:57:23 | 
| 言語 | PyPy3  (7.3.15)  | 
                    
| 結果 | 
                             
                                AC
                                 
                             
                            
                         | 
                    
| 実行時間 | 1,789 ms / 3,000 ms | 
| コード長 | 632 bytes | 
| コンパイル時間 | 323 ms | 
| コンパイル使用メモリ | 82,304 KB | 
| 実行使用メモリ | 101,248 KB | 
| 最終ジャッジ日時 | 2024-12-22 12:21:18 | 
| 合計ジャッジ時間 | 18,380 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge2 / judge3 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 2 | 
| other | AC * 29 | 
ソースコード
import sys
input = sys.stdin.readline
H, W = map(int, input().split())
S = [input()[:-1] for _ in range(H)]
now = {(0, 0)}
ans = [S[0][0]]
for _ in range(H+W-2):
    m = 'z'
    nex = set()
    
    for x, y in now:
        if x+1<H:
            if m==S[x+1][y]:
                nex.add((x+1, y))
            elif m>S[x+1][y]:
                m = S[x+1][y]
                nex = {(x+1, y)}
        
        if y+1<W:
            if m==S[x][y+1]:
                nex.add((x, y+1))
            elif m>S[x][y+1]:
                m = S[x][y+1]
                nex = {(x, y+1)}
    
    ans.append(m)
    now = nex
print(''.join(ans))