結果

問題 No.20 砂漠のオアシス
ユーザー lloyz
提出日時 2023-10-06 01:00:06
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 210 ms / 5,000 ms
コード長 872 bytes
コンパイル時間 265 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 79,844 KB
最終ジャッジ日時 2024-07-26 15:15:59
合計ジャッジ時間 3,298 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 21
権限があれば一括ダウンロードができます

ソースコード

diff #

from heapq import heappop, heappush

n, v, oj, oi = map(int, input().split())
oj -= 1
oi -= 1
L = [list(map(int, input().split())) for _ in range(n)]

Directions = [(1, 0), (0, 1), (-1, 0), (0, -1)]
DP = [[0 for _ in range(n)] for _ in range(n)]
DP[0][0] = v
H = [(-v, 0, 0, 0)]
while H:
    cv, ci, cj, use = heappop(H)
    cv *= -1
    if cv < DP[ci][cj]:
        continue
    if ci == n - 1 and cj == n - 1:
        break
    for di, dj in Directions:
        ni, nj = ci + di, cj + dj
        nuse = use
        if 0 <= ni < n and 0 <= nj < n:
            nv = cv - L[ni][nj]
            if not use and ni == oi and nj == oj:
                nv *= 2
                nuse = True
            if nv <= DP[ni][nj]:
                continue
            DP[ni][nj] = nv
            heappush(H, (-nv, ni, nj, nuse))
if DP[-1][-1] == 0:
    print("NO")
else:
    print("YES")
0