結果

問題 No.34 砂漠の行商人
ユーザー しらっ亭しらっ亭
提出日時 2015-08-15 04:50:43
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 911 bytes
コンパイル時間 537 ms
コンパイル使用メモリ 10,956 KB
実行使用メモリ 272,188 KB
最終ジャッジ日時 2023-09-25 11:54:48
合計ジャッジ時間 11,239 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 20 ms
8,676 KB
testcase_01 AC 20 ms
8,660 KB
testcase_02 AC 43 ms
9,444 KB
testcase_03 AC 19 ms
8,668 KB
testcase_04 AC 570 ms
24,720 KB
testcase_05 AC 563 ms
24,976 KB
testcase_06 AC 44 ms
9,380 KB
testcase_07 AC 954 ms
37,072 KB
testcase_08 AC 1,329 ms
61,976 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 defaultdict
import heapq


def solve():
    N, V, Sx, Sy, Gx, Gy = list(map(int, input().split()))
    L = []
    for i in range(N):
        L.append(list(map(int, input().split())))
    Sy -= 1
    Sx -= 1
    Gy -= 1
    Gx -= 1

    dy = (-1, 0, 1, 0)
    dx = (0, -1, 0, 1)

    dist = defaultdict(lambda: float('inf'))
    hq = []
    heapq.heappush(hq, (0, Sy, Sx, V))

    while hq:
        n, y, x, v = heapq.heappop(hq)

        if y == Gy and x == Gx:
            print(n)
            return

        for d in range(4):
            ny = y + dy[d]
            nx = x + dx[d]
            if 0 <= nx < N and 0 <= ny < N and L[ny][nx] < v:
                nv = v - L[ny][nx]
                if dist[ny, nx, nv] > n + 1:
                    dist[ny, nx, nv] = n + 1
                    heapq.heappush(hq, (n + 1, ny, nx, nv))

    print(-1)


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