結果

問題 No.34 砂漠の行商人
ユーザー 👑 rin204rin204
提出日時 2022-07-10 16:56:54
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 952 bytes
コンパイル時間 352 ms
コンパイル使用メモリ 10,888 KB
実行使用メモリ 466,104 KB
最終ジャッジ日時 2023-09-01 23:59:12
合計ジャッジ時間 12,817 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 19 ms
12,944 KB
testcase_01 AC 20 ms
8,560 KB
testcase_02 AC 56 ms
8,596 KB
testcase_03 AC 20 ms
8,792 KB
testcase_04 AC 791 ms
11,388 KB
testcase_05 AC 833 ms
12,240 KB
testcase_06 AC 63 ms
8,748 KB
testcase_07 AC 1,381 ms
14,036 KB
testcase_08 AC 1,867 ms
17,552 KB
testcase_09 TLE -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
権限があれば一括ダウンロードができます

ソースコード

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