結果
| 問題 |
No.2064 Smallest Sequence on Grid
|
| コンテスト | |
| ユーザー |
shobonvip
|
| 提出日時 | 2022-09-02 22:02:59 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 733 ms / 3,000 ms |
| コード長 | 825 bytes |
| コンパイル時間 | 156 ms |
| コンパイル使用メモリ | 82,028 KB |
| 実行使用メモリ | 160,496 KB |
| 最終ジャッジ日時 | 2024-11-16 03:30:41 |
| 合計ジャッジ時間 | 8,608 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 29 |
ソースコード
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]))
shobonvip