結果
| 問題 | 
                            No.2064 Smallest Sequence on Grid
                             | 
                    
| コンテスト | |
| ユーザー | 
                             shobonvip
                         | 
                    
| 提出日時 | 2022-09-02 21:58:58 | 
| 言語 | PyPy3  (7.3.15)  | 
                    
| 結果 | 
                             
                                TLE
                                 
                             
                            
                         | 
                    
| 実行時間 | - | 
| コード長 | 822 bytes | 
| コンパイル時間 | 286 ms | 
| コンパイル使用メモリ | 82,156 KB | 
| 実行使用メモリ | 208,544 KB | 
| 最終ジャッジ日時 | 2024-11-16 03:23:44 | 
| 合計ジャッジ時間 | 59,762 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge5 / judge2 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 2 | 
| other | AC * 11 WA * 4 TLE * 14 | 
ソースコード
h,w = map(int,input().split())
s = [input() for i in range(h)]
# BFSっぽいのをする. (ただし少し特殊)
mada = [(0,0)]
t = [0] * h*w
t[0] = 1
while mada:
	k = 26
	for x, y in mada:
		if x < h-1:
			k = min(k, ord(s[x+1][y]) - ord("a"))
		if y < w-1:
			k = min(k, ord(s[x][y+1]) - ord("a"))
	fr = []
	for i in mada:
		if x < h-1:
			if ord(s[x+1][y]) - ord("a") == k:
				if t[(x+1)*w + y] == 0:
					fr.append((x+1, y))
					t[(x+1)*w + y] = 1
		if y < w-1:
			if ord(s[x][y+1]) - ord("a") == k:
				if t[x*w + y+1] == 0:
					fr.append((x, y+1))
					t[x*w + y+1] = 1
	mada = fr
ans = []
x = h-1; y = w-1
while x > 0 or y > 0:
	if x > 0 and t[(x-1)*w + y] == 1:
		ans.append(s[x][y])
		x -= 1
	elif y > 0 and t[x*w + y-1] == 1:
		ans.append(s[x][y])
		y -= 1
ans.append(s[0][0])
print("".join(ans[::-1]))
            
            
            
        
            
shobonvip