結果

問題 No.34 砂漠の行商人
ユーザー rin204
提出日時 2022-07-10 16:56:54
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
MLE  
実行時間 -
コード長 952 bytes
コンパイル時間 832 ms
コンパイル使用メモリ 12,416 KB
実行使用メモリ 803,508 KB
最終ジャッジ日時 2025-01-03 13:35:23
合計ジャッジ時間 62,611 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 17 TLE * 7 MLE * 2
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque

n, v, sy, sx, gy, gx = map(int, input().split())
L = [list(map(int, input().split())) for _ in range(n)]
sx -= 1
sy -= 1
gx -= 1
gy -= 1
dist = [-1] * (n * n * v)
def f(i, j, k):
    return (i * n + j) * v + k
dist[f(sx, sy, v - 1)] = 0
queue = deque()
queue.append(f(sx, sy, v - 1))
directions = [(0, 1), (0, -1), (1, 0), (-1, 0)]
while queue:
    tmp = queue.popleft()
    x = tmp // (n * v)
    tmp -= x * n * v
    y = tmp // v
    vv = tmp - y * v
    
    for dx, dy in directions:
        nx = x + dx
        ny = y + dy
        if nx == -1 or nx == n or ny == -1 or ny == n:
            continue
        nv = vv - L[nx][ny]
        if nv < 0:
            continue
        if dist[f(nx, ny, nv)] == -1:
            if nx == gx and ny == gy:
                print(dist[f(x, y, vv)] + 1)
                exit()
            dist[f(nx, ny, nv)] = dist[f(x, y, vv)] + 1
            queue.append(f(nx, ny, nv))
print(-1)
0