結果

問題 No.124 門松列(3)
コンテスト
ユーザー norioc
提出日時 2026-01-03 01:45:23
言語 PyPy3
(7.3.17)
結果
AC  
実行時間 89 ms / 5,000 ms
コード長 1,002 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 727 ms
コンパイル使用メモリ 82,704 KB
実行使用メモリ 77,384 KB
最終ジャッジ日時 2026-01-03 01:45:37
合計ジャッジ時間 4,697 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

from collections import defaultdict, deque


def neighbors4(r: int, c: int) -> list[tuple[int, int]]:
    res = []
    for dr, dc in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
        nr = r + dr
        nc = c + dc
        if not (0 <= nr < H and 0 <= nc < W): continue
        res.append((nr, nc))

    return res


def is_kadomatu(a, b, c) -> bool:
    if a == c: return False
    if a < b > c: return True
    if a > b < c: return True
    return False


INF = 1 << 62
W, H = map(int, input().split())
G = []
for _ in range(H):
    G.append(list(map(int, input().split())))

used = set()
dists = defaultdict(lambda: INF)
q = deque([
    (1, 1, 0, G[0][0]),
    (1, 0, 1, G[0][0])
    ])

while q:
    step, r, c, x = q.popleft()
    if r == H-1 and c == W-1:
        print(step)
        break

    if (r, c, x) in used: continue
    used.add((r, c, x))

    for nr, nc in neighbors4(r, c):
        if is_kadomatu(G[nr][nc], G[r][c], x):
            q.append((step+1, nr, nc, G[r][c]))
else:
    print(-1)
0