結果

問題 No.124 門松列(3)
ユーザー cologne
提出日時 2022-02-28 21:45:53
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 877 bytes
コンパイル時間 157 ms
コンパイル使用メモリ 82,380 KB
実行使用メモリ 71,672 KB
最終ジャッジ日時 2024-07-07 01:02:49
合計ジャッジ時間 2,356 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 23 WA * 3
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque


def main():
    W, H = map(int, input().split())
    M = [list(map(int, input().split())) for i in range(H)]
    Q = deque([(False, 0, 0), (True, 0, 0)])
    D = [[[None]*W for i in range(H)] for j in range(2)]
    D[0][0][0] = D[1][0][0] = 0
    while len(Q) > 0:
        b, x, y = Q.popleft()
        if (x, y) == (H-1, W-1):
            print(D[b][x][y])
            return
        for dx, dy in [(-1, 0), (1, 0), (0, 1), (0, -1)]:
            r, c = x + dx, y + dy
            if 0 <= r < H and 0 <= c < W and D[not b][r][c] is None:
                if b and M[x][y] >= M[r][c]:
                    continue
                if (not b) and M[x][y] <= M[r][c]:
                    continue
                D[not b][r][c] = D[b][x][y] + 1
                Q.append((not b, r, c))
    print(-1)
    return


if __name__ == '__main__':
    main()
0