結果

問題 No.2064 Smallest Sequence on Grid
ユーザー ytft
提出日時 2022-09-03 19:40:44
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,491 ms / 3,000 ms
コード長 567 bytes
コンパイル時間 416 ms
コンパイル使用メモリ 81,920 KB
実行使用メモリ 102,656 KB
最終ジャッジ日時 2024-11-17 10:38:44
合計ジャッジ時間 13,520 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 29
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input=lambda:sys.stdin.readline().rstrip()
def compare(a,b):
	return a if a<b else b
H,W=map(int,input().split())
S=[input() for i in range(H)]
ans=S[0][0]
pointer=[0]
for j in range(H+W-2):
	cand='{'
	new=set()
	for k in pointer:
		i=[k//W,k%W]
		if i[0]<H-1:
			cand=compare(cand,S[i[0]+1][i[1]])
		if i[1]<W-1:
			cand=compare(cand,S[i[0]][i[1]+1])
	for k in pointer:
		i=[k//W,k%W]
		if i[0]<H-1 and S[i[0]+1][i[1]]==cand:
			new.add((i[0]+1)*W+i[1])
		if i[1]<W-1 and S[i[0]][i[1]+1]==cand:
			new.add(i[0]*W+i[1]+1)
	pointer=new
	ans+=cand
print(ans)
0