結果

問題 No.2064 Smallest Sequence on Grid
ユーザー shobonvipshobonvip
提出日時 2022-09-02 22:02:59
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 733 ms / 3,000 ms
コード長 825 bytes
コンパイル時間 290 ms
コンパイル使用メモリ 87,120 KB
実行使用メモリ 161,540 KB
最終ジャッジ日時 2023-08-10 04:05:24
合計ジャッジ時間 10,639 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 69 ms
71,316 KB
testcase_01 AC 67 ms
71,376 KB
testcase_02 AC 68 ms
71,288 KB
testcase_03 AC 68 ms
71,244 KB
testcase_04 AC 66 ms
71,464 KB
testcase_05 AC 67 ms
71,320 KB
testcase_06 AC 68 ms
71,232 KB
testcase_07 AC 67 ms
71,288 KB
testcase_08 AC 69 ms
71,344 KB
testcase_09 AC 68 ms
71,324 KB
testcase_10 AC 68 ms
71,272 KB
testcase_11 AC 71 ms
71,288 KB
testcase_12 AC 71 ms
71,148 KB
testcase_13 AC 80 ms
76,076 KB
testcase_14 AC 82 ms
76,104 KB
testcase_15 AC 81 ms
76,084 KB
testcase_16 AC 83 ms
76,112 KB
testcase_17 AC 181 ms
156,356 KB
testcase_18 AC 176 ms
156,220 KB
testcase_19 AC 179 ms
156,276 KB
testcase_20 AC 569 ms
159,692 KB
testcase_21 AC 613 ms
160,088 KB
testcase_22 AC 452 ms
158,484 KB
testcase_23 AC 462 ms
159,228 KB
testcase_24 AC 479 ms
159,476 KB
testcase_25 AC 477 ms
159,052 KB
testcase_26 AC 727 ms
161,488 KB
testcase_27 AC 733 ms
161,540 KB
testcase_28 AC 174 ms
156,272 KB
testcase_29 AC 608 ms
148,172 KB
testcase_30 AC 192 ms
150,680 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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 x, y 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]))
0